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 › NullPointerExciption
Page Index Toggle Pages: 1
NullPointerExciption (Read 263 times)
NullPointerExciption
Dec 2nd, 2008, 4:57pm
 
"NullPointerExciption" is what I get when I try running the following code:


int count = 20;
point[] points = new point[count];

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

void draw() {
 background(255);
 stroke(0);
 fill(0);
 for(int temp = 0; temp != count - 1; temp++) {
   points[temp].refresh();
 }
}

class point {
 
 float degree;
 int x; int y;
 int movex; int movey;
 
 point () {
   x = 100;
   y = 100;
   movex = 0;
   movey = 0;
   degree = 0;
 }
 
 void refresh() {
   point(x, y, 10);
 }
}

The exact error is:

Exception in thread "Animation Thread" java.lang.NullPointerException

at sketch_dez02a.draw(sketch_dez02a.java:31)

at processing.core.PApplet.handleDraw(PApplet.java:1406)

at processing.core.PApplet.run(PApplet.java:1311)

at java.lang.Thread.run(Unknown Source)

I can't find the error...
Re: NullPointerExciption
Reply #1 - Dec 2nd, 2008, 4:59pm
 
point[] points = new point[count];

this line only creates an array-object which is able to hold 20 objects of type 'point'.
the array is only filled with 'null' after creation, you have to create the 'point'-objects by yourself, e.g.:

void setup() {
 size(...);
 for(int i = 0; i < points.length; i++) {
   points[i] = new point();
 }
}
Page Index Toggle Pages: 1