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 › What does "NullPointerException" mean
Page Index Toggle Pages: 1
What does "NullPointerException" mean? (Read 596 times)
What does "NullPointerException" mean?
Jun 16th, 2009, 10:11am
 
I get this error message somewhat frequently, but I have no idea what it means. Would anyone happen to know what generally causes this to pop up?
Re: What does "NullPointerException" mean?
Reply #1 - Jun 16th, 2009, 10:25am
 
Your calling something that doesn't exist I think.
Re: What does "NullPointerException" mean?
Reply #2 - Jun 16th, 2009, 2:04pm
 
Simple: you create a variable supposed to hold an object, but omit to initialize it. When you call a method on it or try to access one of its fields, you get a NullPointerException. Which is funny, now that I think of it because AFAIK nowhere else in Java they talk about pointers.
Variables of type object are actually just a reference to an object. If they are declared at class level (or global level in Processing), they are initialized by default at null, ie. pointing nowhere.

Some common cases:

PVector v; // Missing v = new PVector();
v.x = 3;

PGraphics g; // Missing g = createGraphics();
g.beginDraw();

PImage i = loadImage("non existing");
image(img...);

int[] ar; // Missing ar = new int[20];
ar[0] = 1;

PVector[] va = new PVector[10];
va[0].x = 1; // Missing va[0] = new PVector();
Re: What does "NullPointerException" mean?
Reply #3 - Jun 16th, 2009, 6:14pm
 
oooooh. thanks!
Page Index Toggle Pages: 1