Getting errors working with String Arrays

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.

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:


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
1 Like

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:

3 Likes

Do you mean doing something like this?

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);
   }
}

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];

1 Like

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

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:

ArrayIndexOutOfBoundsException: 0

Is the text file empty?

2 Likes

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

2 Likes

That’s great to hear!

1 Like

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?

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?

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

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);
  }
}

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


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);
  }
}
1 Like

shorter version


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++;
}
//
1 Like

shorter version

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++;
}
//
2 Likes

Thanks! Now it works and it aesthetically pleasing :grin:

1 Like