We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Serial.list question
Page Index Toggle Pages: 1
Serial.list question (Read 634 times)
Serial.list question
Apr 16th, 2009, 7:32am
 
How do I trap the Array Out of bounds error if no com ports are found?
Code:

void setup()
{
 println(Serial.list());
 myPort = new Serial(this, Serial.list()[0], 9600);
 myPort.bufferUntil(sc);
 

If I run the sketch and no ports are available I get this error;
Code:

Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version   = RXTX-2.1-7

Exception in thread "Animation Thread" java.lang.ArrayIndexOutOfBoundsException: 0
at Test.setup(Test.java:34)
at processing.core.PApplet.handleDraw(PApplet.java:1400)
at processing.core.PApplet.run(PApplet.java:1328)
at java.lang.Thread.run(Unknown Source)


How can I trap this? I have been looking threw the referance but can't find anything and everything I try gives me other errors.

Thanks
Re: Serial.list question
Reply #1 - Apr 16th, 2009, 8:21am
 
Test for the size of the returned array:
Code:

if (Serial.list().length > 0) {
//then go ahead
} else {
//none available
}
Re: Serial.list question
Reply #2 - Apr 16th, 2009, 8:39am
 
NoahBuddy wrote on Apr 16th, 2009, 8:21am:
Test for the size of the returned array:
Code:

if (Serial.list().length > 0) {
 //then go ahead
} else {
 //none available
}


Hay thanks, why can I not find length in the referance, or the srial lib?
Re: Serial.list question
Reply #3 - Apr 16th, 2009, 9:48am
 
Jassper wrote on Apr 16th, 2009, 8:39am:
why can I not find length in the referance, or the srial lib
Serial.list() returns a Java array. length is a standard feature / property of these arrays.
Re: Serial.list question
Reply #4 - Apr 16th, 2009, 10:04am
 
PhiLho  wrote on Apr 16th, 2009, 9:48am:
Jassper wrote on Apr 16th, 2009, 8:39am:
why can I not find length in the referance, or the srial lib
Serial.list() returns a Java array. length is a standard feature / property of these arrays.

Ah ok, I don't know enough about all this, just enough to get in trouble  Cheesy
But I learn fast  Cool
Page Index Toggle Pages: 1