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 › Null pointer exception with this code
Page Index Toggle Pages: 1
Null pointer exception with this code? (Read 1056 times)
Null pointer exception with this code?
May 19th, 2010, 8:15am
 
I'm using a Processing user interface to control a robot via bluetooth. I get "Exception in thread "Animation Thread" java.lang.NullPointerException
     at GUI$Bluetooth.checkTestButtonClick(GUI.java:231)
     at GUI.mousePressed(GUI.java:101)
     at processing.core.PApplet.handleMouseEvent(PApplet.java:1607)
     at processing.core.PApplet.dequeueMouseEvents(PApplet.java:1544)
     at processing.core.PApplet.handleDraw(PApplet.java:1436)
     at processing.core.PApplet.run(PApplet.java:1327)
     at java.lang.Thread.run(Thread.java:613)"

Note that I only get this error when I click the forward button, not when it is launched.

Line 231 is "void drawBt()", which is the method that draws everything to be used by thr Bluetooth:

Code:

void drawBt() //Draws all things related to bluetooth
{

if (testButtonMouseOver)
{
fill(0, 102, 153);
}
PFont font;
font = loadFont("BankGothic-Light-20.vlw");
textFont(font);
text("Test LED", BLUETOOTH_TEST_BUTTON_Label_X, BLUETOOTH_TEST_BUTTON_Label_Y);
rect(BLUETOOTH_TEST_BUTTON_X, BLUETOOTH_TEST_BUTTON_Y, STANDARD_BUTTON_WIDTH, STANDARD_BUTTON_HEIGHT); //Draw Bluetooth Test button
//fill(STANDARD_BUTTON_COLOR); //Fill button

text("Left", BLUETOOTH_LEFT_BUTTON_LABEL_X, BLUETOOTH_LEFT_BUTTON_LABEL_Y);
rect(BLUETOOTH_LEFT_BUTTON_X, BLUETOOTH_LEFT_BUTTON_Y, STANDARD_BUTTON_WIDTH, STANDARD_BUTTON_HEIGHT);

text("Right", BLUETOOTH_RIGHT_BUTTON_LABEL_X, BLUETOOTH_RIGHT_BUTTON_LABEL_Y);
rect(BLUETOOTH_RIGHT_BUTTON_X, BLUETOOTH_RIGHT_BUTTON_Y, STANDARD_BUTTON_WIDTH, STANDARD_BUTTON_HEIGHT);

text("Forward", BLUETOOTH_FORWARD_BUTTON_LABEL_X, BLUETOOTH_FORWARD_BUTTON_LABEL_Y);
rect(BLUETOOTH_FORWARD_BUTTON_X, BLUETOOTH_FORWARD_BUTTON_Y, STANDARD_BUTTON_WIDTH, STANDARD_BUTTON_HEIGHT);

// DRAW RADAR STUFF

//ellipse(RADAR_X, RADAR_Y, RADAR_WIDTH, RADAR_HEIGHT);

//fill(0);
//ellipse(RADAR_X, RADAR_Y, RADAR_CENTER_HEIGHT, RADAR_CENTER_WIDTH);
//fill(255);

//r.draw();

}


Here's a sample from the checkMouseClick() method:

Code:

void checkTestButtonClick()
{
//println("Button Click Tested!");

if (mouseX > BLUETOOTH_TEST_BUTTON_X && mouseX < BLUETOOTH_TEST_BUTTON_X + STANDARD_BUTTON_WIDTH &&
mouseY > BLUETOOTH_TEST_BUTTON_Y && mouseY < BLUETOOTH_TEST_BUTTON_Y + STANDARD_BUTTON_HEIGHT)
{
println("Test Button CLicked!");

myPort.write('A'); // Sends 'A' char to on board computer to be processed and interpreted
delay(TIME_BEFORE_RESET);
myPort.write('Z');
}


This is the robot's code:

Code:


char val; // Data received from the serial port
int ledPin = 13; // Set the pin to digital I/O 4

int leftMotorPin = 8;
int rightMotorPin = 9;

void setup() {
pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
Serial.begin(115200); // Start serial communication at 9600 bps

//Declare the purpose and use of pin 8 and 9 for motors (output)
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
}

void loop() {
if (Serial.available()) { // If data is available to read,
val = Serial.read(); // read it and store it in val
}
if (val == 'A') { // If A was received
digitalWrite(ledPin, HIGH); // turn the LED on
delay(400);
digitalWrite(ledPin, LOW); // turn the LED on
}
else {
digitalWrite(ledPin, LOW); // Otherwise turn it OFF
}

if (val == 'B') //If Left Motor activated
{
digitalWrite(leftMotorPin, HIGH);
delay(1000);
digitalWrite(leftMotorPin, LOW);
}
else
{
digitalWrite(leftMotorPin, LOW);
}

if (val == 'C') //If right Motor activated
{
digitalWrite(rightMotorPin, HIGH);
delay(1000);
digitalWrite(rightMotorPin, LOW);
}
else
{
digitalWrite(rightMotorPin, LOW);
}

if (val == 'D') //If right and left Motor activated
{
digitalWrite(rightMotorPin, HIGH);
digitalWrite(leftMotorPin, HIGH);

delay(1000);

digitalWrite(rightMotorPin, LOW);
digitalWrite(leftMotorPin, LOW);
}
else
{
digitalWrite(rightMotorPin, LOW);
digitalWrite(leftMotorPin, LOW);
}

if (val == 'Z') //If right and left Motor activated
{
digitalWrite(rightMotorPin, LOW);
digitalWrite(leftMotorPin, LOW);
}

delay(10); // Wait 100 milliseconds for next reading
}


The bluetooth blinks red, showing that it is on, however it does not turn green when GUI is launched. This means that a Bluetooth connection was never established. Can someone help me figure out why?
Re: Null pointer exception with this code?
Reply #1 - May 19th, 2010, 8:52am
 
> Line 231 is "void drawBt()", which is the method that draws everything to be used by thr Bluetooth

that doesn't make sense - that's not the kind of line that'd throw a NPE (usually it's thing.method() that does)

this may be because the lines that get added to the code to convert it to java before compiling is distorting the line numbers. try exporting and looking for the .java code in the created directory. the line you want is probably a few lines above the ones you posted.
Re: Null pointer exception with this code?
Reply #2 - May 19th, 2010, 10:05am
 
koogy is right, the stack trace gives an hint where the problem happens: in the checkTestButtonClick() method.
Fortunately, it is quite short, so I can guess you have the issue because myPort is null.
Re: Null pointer exception with this code?
Reply #3 - May 20th, 2010, 10:24am
 
Ok, so I added some printlns to the forward button code which throws the NPE:

Code:

if (mouseX > BLUETOOTH_FORWARD_BUTTON_X && mouseX < BLUETOOTH_FORWARD_BUTTON_X + STANDARD_BUTTON_WIDTH &&
mouseY > BLUETOOTH_FORWARD_BUTTON_Y && mouseY < BLUETOOTH_FORWARD_BUTTON_Y + STANDARD_BUTTON_HEIGHT)
{
println("Forward Button CLicked!");

myPort.write('D'); // Sends 'B' char to on board computer to be processed and interpreted
println("Written D");
delay(TIME_BEFORE_RESET);
myPort.write('Z');
println("Written Z");
}
}


I get "Forward button clicked!" but I do NOT get "Written D" or "Written Z". meaning something is wrong with portName.

Here is the initializeSerial method:

Code:

void initializeSerial()
{
//Print serial data and create myPort, the port used for hardware connection

println(Serial.list());
String portName = Serial.list()[0];

if (portName == null)
{
println("portName is null!!!");
}
}


I do not get "portName is null!!!", so then what is the problem?

Also, its worth saying that even when the bluetooth module is OFF, the computer still prints it out with println(Serial.list()); which means my computer thinks its there when its not...
Re: Null pointer exception with this code?
Reply #4 - May 20th, 2010, 12:38pm
 
You get a portName, but how do you use it? How myPort is created? That's the one which is null, not portName.
Page Index Toggle Pages: 1