|
Author |
Topic: NullPointerException (Read 320 times) |
|
Jordan J
|
NullPointerException
« on: Feb 10th, 2003, 12:18am » |
|
I seem to get getting a NullPointerException in this code. Could someone perhaps tell me why? int W = 640; int H = 480; float Scale = 10; float sub = 0.5; float num = 1; float substep = 0.01; float numstep = 0.01; float sunx = 0; float suny = 0; Planet roger = new Planet(); void setup() { size(W, H); noBackground(); noStroke(); fill(color(0, 0, 0)); rect(0, 0, W, H); stroke(#FFFFFF); translate(W, H); } void loop() { //stroke(roger.colour); //point(roger.x, roger.y); //point(sunx, suny); } class Planet { float x, y; float xdelta, ydelta; color colour = color(90, 70, 120); Planet() { x = random(0-(W/2), W); y = random(0-(H/2), H); xdelta = 0; ydelta = 0; } void Calcdelta(float xdist, float ydist, float xdelta, float ydelta) { float delta = 0; float dist = sqrt(pow(xdist, 2) + pow(ydist, 2)); if (dist != 0) { delta = (num / (1 + dist)) - sub; xdelta = (xdist / dist) * delta; ydelta = (ydist / dist) * delta; } } } void checkKeys() { if (keyPressed) { //Subtractor if (key == 'T' || key == 't') sub += substep; if (key == 'G' || key == 'g') sub -= substep; //Numerator if (key == 'Y' || key == 'y') num += numstep; if (key == 'H' || key == 'h') num -= numstep; //Scale if (key == 'U' || key == 'u') Scale += 0.01; if (key == 'J' || key == 'j') { Scale -= 0.01; if (Scale <= 0) Scale = 0.01; } //Randomize if (key == 'F' || key == 'f') //randomize(); //Stop if (key == 'Q' || key == 'q') stop(); } }
|
|
|
|
Glen Murphy
|
Re: NullPointerException
« Reply #1 on: Feb 10th, 2003, 1:44am » |
|
One thing, you can't use size(W, H); - you have to put actual integers in there (size(640,480); for example), this is a limitation of P5. Also, you have random() inside your constructor for Planet - at the moment, you can't have P5 functions inside constructors. Using a process of commenting stuff out until it works, your NPE is caused by this line: Code: color colour = color(90, 70, 120); |
| Which is a bit weird, because that line in a P5 by itself runs fine, but whereever I put it in your code, it fails. After playing with this for a while, I get a P5 error while compiling, which is even a bit more odd. Perhaps this should be moved to bugs? Don't have time to investigate further, sorry.
|
|
|
|
|