Array Index Out Of Bounds Exception: using Network library

Hello! I made a sketch that read accelerometers data from an application in my phone. I use TCP protocol to transfer data. The code converts the string that app gives into 3 float values (x,y,z) and store them in a class (accelerometer). I receive data but after some time appears the error: ArrayIndexOutOfBoundsException:1 or ArrayIndexOutOfBoundsException:2 (I attach an image with the error)

Someone knows why this happen?

This is my code:

 import processing.net.*;

int port = 5204;
Server myServer;

Accelerometer accelerometer;

void setup()
{
  size(400, 400);
  background(0);
  myServer = new Server(this, port);
  accelerometer = new Accelerometer();
}

void draw()
{
  // Get the next available client
  Client thisClient = myServer.available();
  // If the client is not null, and says something, display what it said
  if (thisClient !=null) {

    String whatClientSaid= thisClient.readString();
    if (whatClientSaid != null){

    //divido la stringa che arriva in 3 i valori sono separati da "," e la stringa termina con "#"
    String[] data = splitTokens(whatClientSaid,",#");//the string is like { -0.32568878,-0.56987789,9.32588427#}

    //Store the 3 string in accelerometer class and convert string to float

        accelerometer.x = float(data[0]);
        accelerometer.y = float(data[1]);
        accelerometer.z = float(data[2]);

        println(whatClientSaid); //stampo la stringa che arriva dal sensore
        println("x: "+ accelerometer.x); //valore (float) dell'accelerazione lungo x
        println("y: "+ accelerometer.y);
        println("z: "+ accelerometer.z);
        println(data.length);
        println();

    }
  } 
}

class Accelerometer {
  float x;
  float y;
  float z;

  Accelerometer(){
    x = 0;
    y = 0;
    z = 0;
  }
}

Error

Tagged:

Answers

  • edited November 2016 Answer ✓

    It is ArrayIndexOutOfBoundsException because trying to access the data[] array at index 1 is out of bounds. This data[] array only has a single item in it -- [0]. There is no [1].

    Add println(splitTokens(whatClientSaid,",#")) right above the error to see for yourself what is breaking. Bad data, I'd guess.

  • Thanks for answering! As you said i think that the data aren't so good! I added an if statement to control the length of Data, it works, (at least the code keep running)

     String[] data = splitTokens(whatClientSaid,",#");//the string is like { -0.32568878,-0.56987789,9.32588427#}
    
        //Store the 3 string in accelerometer class and convert string to float
           if(data.length != 3){
            accelerometer.x = 0;
            accelerometer.y = 0;
            accelerometer.z = 0;
           }
           else{
            accelerometer.x = float(data[0]);
            accelerometer.y = float(data[1]);
            accelerometer.z = float(data[2]);
           }
    

    I think is better to assign the previous value rather than 0.

  • I think is better to assign the previous value rather than 0.

    You don't need to assign the previous values -- you previously assigned them! If you aren't going to write 0s, just do this:

    if(data.length== 3){
      accelerometer.x = float(data[0]);
      accelerometer.y = float(data[1]);
      accelerometer.z = float(data[2]);
    }
    
  • Thanks a lot! I didn't think about it

Sign In or Register to comment.