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?