We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, I'm fairly new to coding in general and I'm working on a project for my CS class. Right now, I'm trying to make a rectangle move back on forth across the screen according to arrow-key inputs. The sketch is titled MOMM. When I run it, I get this error:
Exception in thread "Animation Thread" java.lang.NullPointerException at MOMM$Mover.(MOMM.java:50) at MOMM.setup(MOMM.java:23) at processing.core.PApplet.handleDraw(PApplet.java:2371) at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1499) at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:312)
Here's my code:
Mover scene;
int x;
int y;
void setup(){
size (600,300);
scene = new Mover();
}
void draw(){
background(255);
scene.update();
scene.checkEdges();
scene.display();
}
void keyPressed(){
if(key == CODED){
if (keyCode == LEFT){
scene.moveLEFT();
}
if (keyCode == RIGHT){
scene.moveRIGHT();
}
}
}
class Mover{
PVector position;
PVector velocity;
Mover(){
position = new PVector(width, height);
velocity = new PVector(width, height);
}
{
position.x = x;
position.y = y;
}
void update(){
position.add(velocity);
}
void display(){
pushMatrix();
rect(-450,50,900,200);
popMatrix();
}
void checkEdges(){
if (position.x > 0){
position.x = 0;
}
if (position.x > width){
position.x = width;
}
}
void moveLEFT(){
position.x--;
}
void moveRIGHT(){
position.x++;
}
}
I've searched the forum and the web but I can't seem to figure out what is happening here. I would greatly appreciate some help. Thank you so much!
Answers
http://forum.Processing.org/two/discussion/8045/how-to-format-code-and-text
http://forum.Processing.org/two/discussion/8071/why-do-i-get-a-nullpointerexception
Thank you for the response! I'm sorry, this is probably due to me being inexperienced, but I can't identify anything that isn't initialized or has been reset. I think it needs another pair of eyes, could you please look over it (now that I've correctly formatted it)? Thank you!
you need to tell which line was yellow / gave the error
place lines 33&34 after line 38
for moveright want x++
not y
Thanks for the feedback! I updated it. Okay, line 38 is highlighted yellow.
yes
so do what I said
place lines 33&34 after line 38
(in the old code)
the brackets around the old lines 33 & 34 make no sense - delete them
place lines 33&34 after line 38
read the tutorial on oop
you need to make a new PVector before you can give its x field a value
all this takes place in the constructor
Mover
The NPE lies in Mover's initialization block:
B/c position is still
null
when that block is run! :-@Nonetheless, you end up initializing position inside Mover's constructor.
However, Java's initialization blocks happen before constructors. @-)
Also, there are some inconsistencies in your code:
new PVector(width, height);
?key == CODED
is unnecessary btW. ;)Okay, thank you! I am no longer receiving the NPE error.
An online example to inspire you: :bz
http://studio.ProcessingTogether.com/sp/pad/export/ro.91tcpPtI9LrXp
he uses x,y ....
but not really
If nobody else has mentioned it line 53 looks wrong. It will mean that x will always be 0.
Oh, yes
<0
Thank you all so much for your help! My code is now fully functioning and I got a complete product turned in on time. You're awesome!!
Great!
you could post the entire code and we write a review
;-)