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 & HelpElectronics,  Serial Library › arduino to processing NullPointerException
Page Index Toggle Pages: 1
arduino to processing NullPointerException (Read 1426 times)
arduino to processing NullPointerException
May 22nd, 2009, 6:30am
 
Hello, everybody.
First I'm sorry if i posted it wrong, cause i do not know it better, but i will learn.
Second: I got an arduino board with a potentiometer on it and i wanna send this data to processing. Arduino works good but processing always crying with the NullPointerException. I've looked in other forums for it, but it isn't the same problem like mine (they sad systemfont Monaco is off, but ithat isn't my problem).
So here is the arduino code:

int ledPin1 = 13;


int potPin = 2;

int val = 0;

void setup()
{
Serial.begin(9600);
pinMode(ledPin1, OUTPUT);


}

void loop()
{
val = analogRead(potPin)/4;
Serial.println(val);
delay(20);


val = analogRead(potPin);
digitalWrite(ledPin1, HIGH);
delay(val);
digitalWrite(ledPin1, LOW);
delay(val);
}

and processing:
import processing.serial.*;
int potent = 0;
int linefeed = 10;
int ret = 13;
Serial port;





void setup(){
size(800,800);

port = new Serial(this, Serial.list()[Serial.list().length-1], 9600);
}
void draw(){
background(255);
if(port.available() > 0) {
String data = port.readStringUntil(linefeed);
println("String: " + data);

potent = int(data);
}
println(potent);
drawEllipse(potent, potent/10);
}



void drawEllipse(int sizeTemp, int flacker){
smooth();
//noFill();
fill(0);
strokeWeight(1);
ellipse(width/2,height/2, random(sizeTemp-flacker,sizeTemp), random(sizeTemp-flacker, sizeTemp));
}

and know processing is doing:
Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version   = RXTX-2.1-7
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
String: null

Exception in thread "Animation Thread" java.lang.NullPointerException
     at processing.core.PApplet.parseInt(PApplet.java:5617)
     at processing.core.PApplet.parseInt(PApplet.java:5608)
     at potent.draw(potent.java:40)
     at processing.core.PApplet.handleDraw(PApplet.java:1423)
     at processing.core.PApplet.run(PApplet.java:1328)
     at java.lang.Thread.run(Unknown Source)

and potent = int(data); is always marked.

I'm sorry for the bad english, but it will getting better next time
Re: arduino to processing NullPointerException
Reply #1 - May 22nd, 2009, 8:38am
 
The readStringUntil() method will return null if what you are looking for was not found.

http://processing.org/reference/libraries/serial/Serial_readStringUntil_.html
Re: arduino to processing NullPointerException
Reply #2 - May 24th, 2009, 8:19am
 
Thank you for your answer, now there is no error anymore, but the ellipse is not drawn, the sring i got is the same like arduino shows, but the interger, i need to draw, is always 0. Now my question is, how can i get  the nessesary data? I've tried to convert but it didn't work

Heres the Code:

import processing.serial.*;
int potent = 0;
int linefeed = 10;
int ret = 13;
String myString = null;
Serial port;





void setup(){
 size(800,800);
 //MouseX durch Arduino Werte ersetzen

 port = new Serial(this, Serial.list()[0], 9600);
 port.clear();
 myString = port.readStringUntil(linefeed);
 myString = null;
}
void draw(){
 background(255);
 while(port.available() > 0) {
   myString = port.readStringUntil(linefeed);
   if (myString != null){
     println(myString);
   }
   String myString = "23";
   int potent = Integer.parseInt(myString);
 }
 println(potent);
 drawEllipse(potent, potent/10);
}



void drawEllipse(int sizeTemp, int flacker){
 smooth();
 //noFill();
 fill(0);
 strokeWeight(1);
 ellipse(width/2,height/2, random(sizeTemp-flacker,sizeTemp), random(sizeTemp-flacker, sizeTemp));
}
Page Index Toggle Pages: 1