Loading...
Logo
Processing Forum
Why this is saying ArrayIndexOutofBoundsException: 0 



import processing.serial.*;
Serial myPort;

float brightness = 0;

void setup () {
  size (1000, 600);
  colorMode(RGB);
  println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
  }

void draw () {
  background (1);
    while (myPort.available()>0){
      int inByte = myPort.read();
  
  fill(inByte);
  beginShape(QUADS);
  vertex(0, 0);
  vertex(250, 0);
  vertex(250, 150);
  vertex(0, 150);
  endShape(CLOSE);
}

Replies(6)

Well, I don't have an Arduino hooked up so I can't run your code, but it looks like your highlighted line is making an array of length 0:
Copy code
  1. myPort = new Serial(this, Serial.list()[0], 9600);
Hi asimes,

Thanks for your reply. Yes that line is the one that is giving me the array of length 0. Do you know how can I fix it?

Can I use this code for PIC?


Thanks

Regards
I'm not 100% sure it will fix it, by try making that 0 bigger. If that is an array of length 0, then it can't store elements
We have no control over the value returned by Serial.list(), except by fixing the hardware or the drivers...
The reason I am using   myPort = new Serial(this, Serial.list()[0], 9600); is because on one of the examples the code says that using that code any COM PORT when I plug my devices it ill take that new port and uses that one. In this case, I dont have to care about the port number and just write this code. 

// Example by Tom Igoe

import processing.serial.*;

// The serial port:
Serial myPort;

void setup() {
  // List all the available serial ports:
  println(Serial.list());
  // Open the port you are using at the rate you want:
  myPort = new Serial(this, Serial.list()[0], 9600);
}

void draw() {
  while (myPort.available() > 0) {
    int inByte = myPort.read();
    println(inByte);
  }
}


Thanks to everyone !! 
I think I fixed it. 

The reason is that line of code is only usable when any COM port is connected and is new.