ArrayIndexOutOfBoundsException: 1 error when using String[] and split()

Hi,
I am reading temperature values from an ESP8266 (10 temp probes), then putting them into array befor displaying these values however i am getting the ArrayIndexOutOfBoundsException: 1 error when using String[] and split() to put the individual probe values into the string array.

I assume the ArrayIndexOutOfBoundsException: 1 error means that it cant read the array at position 1 but i am not sure how this has occurred. The code is below.

If anybody has any ideas on why this has occurred that would be much appreciated.

Cheers.

Code:

import processing.serial.*;
import java.io.BufferedWriter;
import java.io.FileWriter;

Serial espUSBPort;
PFont myFont;     // The display font

int lf = 10;      // ASCII linefeed 

String[] snsrTempList = {"0","0","0","0","0","0","0","0","0","0"};
String inString="";

String equipment="Incubator";

//-----date & time-----
String timeString;
String dateString;
String completeString = "";

//-----MAIN-----// 
void setup() { 
  size(350, 400);
  background(175);
  
  // create font with the Create Font Tool 
  myFont = createFont("Arial",20,true); 
  textFont(myFont, 20); 
  text("Temperature Testing",5,20);
  
  espUSBPort = new Serial(this, "/dev/ttyUSB0", 115200);
  espUSBPort.bufferUntil(lf); 
  
  myFont = createFont("Arial",16,true);
  textFont(myFont, 16);
  text(equipment,10,50);
  text("Sensor results: ",10,80);
}

void draw() { 
  fill (175);
  rect (30,95,180,200);
  fill(250);
  
  for (int i=0;i<10;i++) {
    text("Sensor" + i,40,110 + i * 20);
    text(snsrTempList[i],120,110 + i * 20);
  }
}

//---------functions---------//    
void serialEvent(Serial p) { 
  if (p == espUSBPort) {
    inString = p.readString();
    println(inString.length());
    if (inString.length() < 62) {
      println(inString);
      snsrTempList = split(inString, ',');
    }
  }
}

Why do you have this?

Did you try to println instring? Is it comma separated?

In draw, initially the array is empty. Run the for loop to its length instead of using 10

Hi Chrisir,
And thnaks for het reply.

the

if (inString.length() < 62) {

is there because sometimes initially the input buffer has to many characters, actual correct data plus some previous data which i don’t want so this simply ignores any strings that are to long.

Yes i have done println(inString) and the result is:

20.88,21.19,20.06,19.94,20.31,19.69,19.56,20.69,20.56,21.25

which as you can see is comma delimited.

I have made a few small changes including creating random input strings for inString to simulate the strings coming from the ESP8266 uC and this works fine.

What seems to be happening is that if the string comes from the ESP8266 it is not being recognized correctly so the split() function doesn’t work leaving snsrTempList empty so any call to

snsrTempList[i]

fails. But i don’t know why this is happening. Is some type of character conversion required before attempting to read from inString. Although println(inString) seems to read it OK and simulated string data is also processed correctly.

Do i need to import the arduino library to be able to read it the incoming characters properly?

Any help with this is much appreciated.

Cheers.

Hello,

I tried different options and could not confirm your suspicion.

String [] list = {"0", "0", "0", "0", "0", "0", "0", "0", "0", "0"};
String elevenList = "0,1,2,3,4,5,6,7,8,9";
String tenList = "0,1,2,3,4,5,6,7,8,9";
String nineList = "0,1,2,3,4,5,6,7,8";
String empty = "";
String missing = null;
printArray(list);
list=split(elevenList, ',');
printArray(list);
list=split(tenList, ',');
printArray(list);
list=split(nineList, ',');
printArray(list);
list=split(empty, ',');
printArray(list);
list= split(missing, ',');
printArray(list);

neither getting a too many, to few, empty or null array from split seems to be a problem…

in my experience an ArrayIndexOutOfBoundsException event happens when you try to access or write to a specific element of an array that is not there
my only suspicion is that, if you get a smaller number of ten values, your text() routine may complain.

you could rule that out by using the current length of your array.

 for (int i=0;i< snsrTempList.length;i++) {
    text("Sensor" + i,40,110 + i * 20);
    text(snsrTempList[i],120,110 + i * 20);
  }

you could get another ArrayIndexOutOfBoundsException if the array = null.
but you can rule that out again by only updating the array if it is not null:

void serialEvent(Serial p) { 
  if (p == espUSBPort) {
    inString = p.readString();
    if (inString.length() < 62 && inString.length != null) {
      snsrTempList = split(inString, ',');
    }
  }
}

hope that helps

Also use if… to check out if data is there