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 & HelpPrograms › What does NullPointerException Mean
Page Index Toggle Pages: 1
What does NullPointerException Mean? (Read 1510 times)
What does NullPointerException Mean?
Nov 28th, 2009, 4:00am
 
Hi,
I'm using the text command, and i am getting NullPointerException fromn the complier, what does this mean? The code is not working so i guess its not good!

Thanks

Pete
Re: What does NullPointerException Mean?
Reply #1 - Nov 28th, 2009, 4:09am
 
so maybe show us your code and we can tell you whats wrong
Re: What does NullPointerException Mean?
Reply #2 - Nov 28th, 2009, 4:17am
 
Hi Cedric,
Again thanks for your support.
here is the code, its a little all over the place as its in development!
I'm trying to read in a string from the serial port and then print it out in draw(), the string is printing out ok, i just cant get the text command to work!

Thanks

Pete

Code:


//Processing code to run with this example:

import processing.serial.*; // import the Processing serial library
Serial myPort; // The serial port

float bgcolor; // Background color
String fgcolor; // Fill color
String xpos, ypos; // Starting position of the ball
char sender = 'A';
PFont font;
String s;
String mystring_ext;

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

// List all the available serial ports
println(Serial.list());

// I know that the first port in the serial list on my mac
// is always my Arduino module, so I open Serial.list()[0].
// Change the 0 to the appropriate number of the serial port
// that your microcontroller is attached to.
myPort = new Serial(this, Serial.list()[1], 9600);

// read bytes into a buffer until you get a linefeed (ASCII 10):
myPort.bufferUntil('\n');

// draw with smooth edges:
smooth();
}

void draw() {
background(0);
fill(150);
font = loadFont("Aharoni-Bold-20.vlw");
textFont(font);
background (0);
//text("xpos", 10, 20);
//text("ypos", 10, 40);
//text("fgcolor", 10, 60);
String s = "The quick brown fox jumped over the lazy dog.";
text(mystring_ext, 0, 0, 200, 200);
fill(0, 102, 153);
}

// serialEvent method is run automatically by the Processing applet
// whenever the buffer reaches the byte value set in the bufferUntil()
// method in the setup():

void serialEvent(Serial myPort) {
// read the serial buffer:
String myString = myPort.readStringUntil('\n');
// if you got any bytes other than the linefeed:
myString = trim(myString);
mystring_ext = myString;

// split the string at the commas
// and convert the sections into integers:
String sensors[] = (split(myString, ','));

// print out the values you got:
for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
print(sensors[sensorNum] + " ");
}
// add a linefeed after all the sensor values are printed:
println();
if (sensors.length > 1) {
xpos = sensors[0];
ypos = sensors[1];
fgcolor = sensors[2];
}

// send a byte to ask for more data:
if (sender == 'A'){
sender = 'B';}
else{
sender = 'A';}
myPort.write(sender);
}


Re: What does NullPointerException Mean?
Reply #3 - Nov 28th, 2009, 5:15am
 
your String is empty when you run draw the first time i guess... so just change String mystring_ext; to String mystring_ext = "";

and btw, dont load the font in draw, load it in setup. there is no need to reload it every frame.
Re: What does NullPointerException Mean?
Reply #4 - Nov 28th, 2009, 8:33am
 
To answer the base question: this is a runtime exception, not a compiler error.
This exception happens when you try to use an object which is not initialized: by default, it is null (if declared globally or at class level). Thus, you get this error when you try to use this non-existent object to get a field or call a method.
That's a very common error...
Page Index Toggle Pages: 1