# Getting errors working with String Arrays

**URL:** <https://discourse.processing.org/t/getting-errors-working-with-string-arrays/16701>\
**Category:** Coding Questions\
**Created:** [December 29, 2019, 1:31pm UTC](https://discourse.processing.org/t/getting-errors-working-with-string-arrays/16701 "2019-12-29T13:31:19Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Salttt](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@Salttt](https://discourse.processing.org/u/Salttt)\
**Post date:** [December 29, 2019, 1:31pm UTC](https://discourse.processing.org/t/getting-errors-working-with-string-arrays/16701/1 "2019-12-29T13:31:19Z")

</div>

Beginner here, I get an error code whenever I try to run this small part of my code. Could someone help, I am trying to load an integer in string form loaded in hs.txt and only replace it when the highscore in the session is higher.

```auto
int sessionhs;
String shs;                                           
String[] hs = {shs};                                           
String[] lasths;               
String ahs1 = lasths[0];                               
int ahs = int(ahs1);

void setup() {
  size(1920, 1080);

  lasths = loadStrings("hs.txt"); 
}

void draw() {
   if (sessionhs > ahs) {
     String[] hs = {shs};
     saveStrings("hs.txt", hs);
   }
}

```

MY ERROR CODE:

```auto

java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
	at processing.core.PApplet.runSketch(PApplet.java:10845)
	at processing.core.PApplet.main(PApplet.java:10613)
Caused by: java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at processing.core.PApplet.runSketch(PApplet.java:10839)
	... 1 more
Caused by: java.lang.NullPointerException
RuntimeException: java.lang.reflect.InvocationTargetException
	at sketch_191229b.<init>(sketch_191229b.java:21)
	... 6 more

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 29, 2019, 1:38pm UTC](https://discourse.processing.org/t/getting-errors-working-with-string-arrays/16701/2 "2019-12-29T13:38:23Z")

</div>

> [@Salttt](#):
>
> ```auto
> String[] lasths;
> String ahs1 = lasths[0];
> 
> ```

B/c field _lasths[]_ hasn’t been explicitly initialized, it contains `null`.

Therefore, you can’t use the array access operator `[]` until you assign a String[] to it:

> **[\[\] (array access) / Reference](https://processing.org/reference/arrayaccess.html)**
>
> The array access operator is used to specify a location within an array. The data at this location can be defined with the syntax array\[element\] = value and read with the syntax value = arra…

---

<div class="post-metadata">

**Author:** ![Salttt](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@Salttt](https://discourse.processing.org/u/Salttt)\
**Post date:** [December 29, 2019, 1:57pm UTC](https://discourse.processing.org/t/getting-errors-working-with-string-arrays/16701/3 "2019-12-29T13:57:14Z")

</div>

Do you mean doing something like this?

```auto
int sessionhs;
String shs;                                           
String[] hs = {shs};                                           
String[] lasths;     
String ahs1 = lasths[0];                               
int ahs = int(ahs1);

void setup() {
  size(1920, 1080);
 lasths = loadStrings("hs.txt"); 
}

void draw() {
  lasths[0] = shs;
   if (sessionhs > ahs) {
     String[] hs = {shs};
     saveStrings("hs.txt", hs);
   }
}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 29, 2019, 2:01pm UTC](https://discourse.processing.org/t/getting-errors-working-with-string-arrays/16701/4 "2019-12-29T14:01:09Z")

</div>

You’re indeed initializing field _lasths[]_ inside **setup()**:  
`lasths = loadStrings("hs.txt");`

However, you still attempt to access _lasths[]_ w/ the array access operator `[]` BEFORE **setup()**:  
`String ahs1 = lasths[0];`

---

<div class="post-metadata">

**Author:** ![Salttt](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@Salttt](https://discourse.processing.org/u/Salttt)\
**Post date:** [December 29, 2019, 2:19pm UTC](https://discourse.processing.org/t/getting-errors-working-with-string-arrays/16701/5 "2019-12-29T14:19:22Z")

</div>

Okay, so I defined all the things, and it looks like this:

```auto
int sessionhs = 0;
String shs = nf(sessionhs);                                         
String[] hs;                                  
String[] lasths;     
String ahs1;                             
int ahs;

void setup() {
  size(1920, 1080);
 lasths = loadStrings("hs.txt"); 
}

void draw() {
  ahs1 = (lasths[0]);
  int ahs = int(ahs1);
   if (sessionhs > ahs) {
     hs[0] = ahs1;
     saveStrings("hs.txt", hs);
   }
}

```

However, with the .txt file present, I get this error message:

```auto
ArrayIndexOutOfBoundsException: 0

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 29, 2019, 2:41pm UTC](https://discourse.processing.org/t/getting-errors-working-with-string-arrays/16701/6 "2019-12-29T14:41:02Z")

</div>

Is the text file empty?

---

<div class="post-metadata">

**Author:** ![Salttt](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@Salttt](https://discourse.processing.org/u/Salttt)\
**Post date:** [December 29, 2019, 2:45pm UTC](https://discourse.processing.org/t/getting-errors-working-with-string-arrays/16701/7 "2019-12-29T14:45:54Z")

</div>

When first starting the program, the text file is empty until the game has been played, from there on it contains a number unless deleted.

EDIT: Adding 0 to the empty text file lets the program run, thank you

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 29, 2019, 2:49pm UTC](https://discourse.processing.org/t/getting-errors-working-with-string-arrays/16701/8 "2019-12-29T14:49:04Z")

</div>

That’s great to hear!

---

<div class="post-metadata">

**Author:** ![Salttt](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@Salttt](https://discourse.processing.org/u/Salttt)\
**Post date:** [December 29, 2019, 2:59pm UTC](https://discourse.processing.org/t/getting-errors-working-with-string-arrays/16701/9 "2019-12-29T14:59:02Z")

</div>

Okay, now I have a different issue; Whenever saveStrings(“hs.txt”, hs); happens, my game crashes and I get the NullPointerException error. Is it because the data from the .txt file is being read and I can’t edit it while its being read?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 29, 2019, 5:10pm UTC](https://discourse.processing.org/t/getting-errors-working-with-string-arrays/16701/10 "2019-12-29T17:10:49Z")

</div>

I guess you need to show your latest version of your code

From what I see above, you don’t copy lasths into hs, so hs is empty?

---

<div class="post-metadata">

**Author:** ![Salttt](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@Salttt](https://discourse.processing.org/u/Salttt)\
**Post date:** [December 29, 2019, 5:25pm UTC](https://discourse.processing.org/t/getting-errors-working-with-string-arrays/16701/11 "2019-12-29T17:25:44Z")

</div>

Okay, I reworked things around a while ago to experiment and now I can read what is in the .txt file, but I still cannot change what is written inside. This is part of a highscore system for a larger game btw

```auto
int sessionhs;
String shs = nf(sessionhs);                             
String[] lasths;                                                               
int ahs;                                            
String ahs1;

void setup() {
  size(1920, 1080);
  lasths = loadStrings("hs.txt"); 
}

void draw() {
  ahs1 = (lasths[0]);
  ahs = int(ahs1);
  if (sessionhs > ahs) {
    ahs = sessionhs;
    ahs1 = shs;
    lasths[0] = ahs1;
    saveStrings("hs.txt", lasths);  
    println("l" + ahs);
  }
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 29, 2019, 5:35pm UTC](https://discourse.processing.org/t/getting-errors-working-with-string-arrays/16701/12 "2019-12-29T17:35:18Z")

</div>

this seems to work… when you make a text file (with 0 being in line 0)

but it gives you only one high score number

```auto

int sessionhs=3334;
//String shs = nf(sessionhs);

String[] lasths;                                                               
int ahs;                                            
String ahs1;

void setup() {
  size(333, 180);
  lasths = loadStrings("hs.txt");
}

void draw() {

  ahs1 = (lasths[0]);
  ahs = int(ahs1);

  if (sessionhs > ahs) {
    ahs = sessionhs;
    // ahs1 = shs;
    lasths[0] = str(sessionhs);
    saveStrings("hs.txt", lasths);  
    println("l" + ahs);
  }
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 29, 2019, 5:48pm UTC](https://discourse.processing.org/t/getting-errors-working-with-string-arrays/16701/13 "2019-12-29T17:48:47Z")

</div>

shorter version

```auto

int sessionhs=4355;

String[] lasths;                                                               

void setup() {
  size(333, 180);
  background(0);

  lasths = loadStrings("hs.txt");
}

void draw() {
  background(0);

  text( lasths[0]
    + "\n\npress any key to change high score ", 
    111, 111);
}

//---------------------------------------------------------------------------------------------

void keyPressed() {
  if (sessionhs > int(lasths[0])) {
    lasths[0] = str(sessionhs);
    saveStrings("hs.txt", lasths);  
    println("saved " + lasths[0]);
  }
  sessionhs++;
}
//

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 29, 2019, 5:50pm UTC](https://discourse.processing.org/t/getting-errors-working-with-string-arrays/16701/14 "2019-12-29T17:50:10Z")

</div>

shorter version

```auto
int sessionhs=4355;

String[] lasths;                                                               

void setup() {
  size(333, 180);
  background(0);

  lasths = loadStrings("hs.txt");
}

void draw() {
  background(0);

  text( lasths[0]
    + "\n\npress any key to change high score ", 
    111, 111);
}

//---------------------------------------------------------------------------------------------

void keyPressed() {
  if (sessionhs > int(lasths[0])) {
    lasths[0] = str(sessionhs);
    saveStrings("hs.txt", lasths);  
    println("saved " + lasths[0]);
  }
  sessionhs++;
}
//

```

---

<div class="post-metadata">

**Author:** ![Salttt](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@Salttt](https://discourse.processing.org/u/Salttt)\
**Post date:** [December 29, 2019, 5:50pm UTC](https://discourse.processing.org/t/getting-errors-working-with-string-arrays/16701/15 "2019-12-29T17:50:10Z")

</div>

Thanks! Now it works and it aesthetically pleasing 😁
