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 a PVector in a Class
Page Index Toggle Pages: 1
Null Pointer Exception With a PVector in a Class (Read 1482 times)
Null Pointer Exception With a PVector in a Class
Jun 5th, 2010, 8:52am
 
I'm using a PVector in a class I designed to model the operation of a movable bridge leaf. I knew that I would have to be careful using classes within classes because I'm relatively new to OOP. I seem to have gotten most of my problems ironed out, but I still get this Null Pointer Exception.

I have recreated the error in a simpler piece of code below. I figure the problem must be something silly like a missing semicolon or an issue with scope. Either way, web searches and looking through the examples here haven't helped. I have found examples that seem to look the same and work, so I must be missing something fundamental.

All I'm expecting to see with this example is '3' printed in the console output.

Code:
Leaf nearLeaf;

void setup() {
 size(100, 100);
 nearLeaf = new Leaf();
 nearLeaf.display();
}

class Leaf {
 PVector forwardCG;
 
 void Leaf() {
   forwardCG = new PVector(3, 4);
 }
 
 void display() {
   println(forwardCG.x);
 }
}
Re: Null Pointer Exception With a PVector in a Class
Reply #1 - Jun 5th, 2010, 9:10am
 
Classical error: constructors has no return type!
So if you drop the "void" before the Leaf() definition, you get rid of the issue.
The problem was that your constructor has no parameters, so Java seeing no constructor in the class just used the default, empty one...
Re: Null Pointer Exception With a PVector in a Class
Reply #2 - Jun 5th, 2010, 8:19pm
 
Excellent! Thanks so much. I knew it would be something simple like that.
Page Index Toggle Pages: 1