got crazy with array...

edited January 2017 in Arduino

hello everyone..... im new to processing and found first problem..... sorry if i ask simple question but i cant understand this...

i got this code:

import processing.serial.*;
 Serial myPort;
 String PORT;
 String result[];

void setup()
{
size(800, 200);
myPort = new Serial(this, Serial.list()[3], 9600);              
//COM that your arduino is on. Mine is on COM22
myPort.bufferUntil('\n');  
}

void serialEvent (Serial myPort)                    //when data is received
{
 PORT = (myPort.readStringUntil('\n'));          //read to the end of the line
 result = split(PORT, ',');
}

void draw()
{
    background(0,0,40);
    fill(255);
    textSize(20);
    textAlign(RIGHT);
    text("Valore Riserva: ", 200, height/2);
    text(result[0], 220, height/2);
    text("Valore Medio: ", 450, height/2);
    //text(result[1], 470, height/2);
    text("Valore Pieno: ", 700, height/2);
    //text(result[2], 720, height/2);
    for ( int i = 0; i < result.length; i = i+1){
      print("index ", i+" : ");
      println(result[i]);
    }
   printArray(result);
}

and i got this result on console:

index  0 : 1
index  1 : 1
index  2 : 1
index  3 : 

[0] "1"
[1] "1"
[2] "1"
[3] ""

everything seems to work but when i call an index like this way:

print(result[1]);

it returns me this error:

ArrayIndexOutOfBoundsException:1

like i was calling an unexisting array element......

am i doing something wrong?

Answers

Sign In or Register to comment.