Line Graph - ArrayIndexOutOfBoundsException: 570

Hello, I’m following a tutorial on making a dynamic linegraph whilst using my own data. I’ve come across an error which I’m unable to fix myself and would really appreciate some fresh eyes on this :slight_smile:

I know I’m doing something wrong with my array values, but I can’t work out where I’m going wrong.

Main Sketch:

String filename = "Simplified 2019 + 2020 Steps Data.csv";
String[] rawData;



void setup(){
processData();
  
}

void draw() {
  
  
  
}

void processData(){
    rawData = loadStrings(filename);
  for(int i = 1; i < rawData.length; i+=9) {
    Year y = new Year();
    for (int j = 1; j < 9; j++) {
   String[] thisRow = split(rawData[i+j], ",");
   println(thisRow[1]);
    
  }
 }
}
  

Tab 2 (“Year”)

class Year {
  
  // variables
  int level;
  int[] months = {1, 2, 3, 4, 5, 6, 7, 8, 9};
 int [] steps = new int[9];
  
  // constructor
  Year() {
 
  }
  // functions
  
}

And for good measure, here is a screenshot of some of my data (since I can’t upload the CSV file):

I know this is something very simple I’m overlooking, but I thank you heaps if you are able to help me on this! :slight_smile:

this is causing your ArrayIndexOutOfBoundsException?

Surround the lines with if (i+j < rawData.length) {…}

because of the i+=9 you will probably go over the end of the array

Thanks so much for getting back to me! Sorry for being dumb, but could you please show me an example? Is it like this:

if(int i = 1; i < rawData.length; i+=9) {
Year y = new Year();
for (int j = 1; j < 9; j++) {
String[] thisRow = split(rawData[i+j], “,”);
println(thisRow[1]);

}
}

I tried it this way, but it is saying I’ve got an unexpected token, I think I’ve done it wrong haha

Do you know if I could change my array number to fix it at all? My months go up to 9, so I’ve tried making the value “8” since I know an array counts from 0, but that didn’t work either





void processData() {
  rawData = loadStrings(filename);
  for (int i = 1; i < rawData.length; i+=9) {
    Year y = new Year();
    for (int j = 1; j < 9; j++) {

      if (i+j < rawData.length) {// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

        String[] thisRow = split(rawData[i+j], ",");
        println(thisRow[1]);
      } // new if/////////////////////////////
    }//for
  }//for
}//func

You are a genius, thank you so much! I really appreciate it :grinning_face_with_smiling_eyes:

1 Like