Problem with "String Index Out Of Bounds Exception"

So I’m writing this code for a small project I’m working and I keep getting the error message “StringIndexOutOfBoundsException: String index out of range: 0”

I won’t post the entire code cause its a bit long but here’s the code of where it happens

if(pulled100 || pulled200 || pulled300){
      for(int i=0; i<data.getRowCount(); i++){
        for(int j=0; j<int(numDays); j++){
          if(data.getInt(i, "selected")==1){
            value=data.getString(i, finalDate+j+int(slider));
            println(value);
            println(value.length());
            println(value.charAt(0));  //Error message occurs here
            int counter=0;
            while(value.charAt(counter)!='/'){    //If I get rid of the top print statement it occurs here
              counter++;
              pull+=value.charAt(counter);
            }
            fill(black);
            textSize(50);
            text(pull, width/2, height/2);
            strokeWeight(width/200);
            stroke(black);
            point(height*9/10-height*int(pull)/graphY, width/20+j/int(numDays));
          }
        }
      }
    }

The console prints out:

58//2
5
5

0

I’m not sure where the last 0 comes from in the console but the above three are the only print statements I have. If anyone could help me out a little bit that would be highly appreciated.

2 Likes

Try

while(value.length > 0  && value.charAt(counter)!=’/’){ 
  counter++;
  pull+=value.charAt(counter);
}
3 Likes

Hey There !

I know your new here but remember to format your code!

1 Like

if (value != null && !value.empty()) println(value.charAt(0));

2 Likes

Okay I will definitely do that next time thanks!

Okay that seemed to do the trick, thank you!