NullPointerException Bug

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

  • edited October 2015

    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

  • Answer ✓

    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

  • edited October 2015

    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

  • edited October 2015 Answer ✓

    The NPE lies in Mover's initialization block:

    {
      position.x = x;
      position.y = y;
    }
    

    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:

    • Why do you declare "global" variables x & y? They're not used anywhere! :-\"
    • Why do you use pushMatrix() & popMatrix() inside display()?
    • Shouldn't rect()'s coordinates be position.x & position.y?
    • Why do you initialize position & velocity w/ new PVector(width, height);?
    • What have position & velocity anything to do w/ dimensions or area sizes?
    • Testing for key == CODED is unnecessary btW. ;)
  • Okay, thank you! I am no longer receiving the NPE error.

  • he uses x,y ....

    but not really

  • edited October 2015
      Mover(){
        position = new PVector(width, height);
        velocity = new PVector(width, height);
           
       
         
        position.x = x;
        position.y = y;
      }
       
    
  • edited October 2015

    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

    ;-)

Sign In or Register to comment.