Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to make a game with Java and in the game, the object that moves side ways called 'Pinko' is supposed to fire small objects called 'pellets' when the up or down arrow keys are pressed. It successfully compiles and runs, but every time I press the up or down arrow key, I get an error saying:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Pinko.move(Pinko.java:75)
    at A2JPanel.actionPerformed(A2JPanel.java:102)
    at javax.swing.Timer.fireActionPerformed(Unknown Source)
    at javax.swing.Timer$DoPostEvent.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

There are seven classes: Application, Constants, JFrame, JPanel, Lovely, Pellet and Pinko.

My code in the move method in Pinko class looks like:

public void move(){
    area.x -= speed;
    if(area.x <= PINKO_MOVE_AREA_LHS || area.x >= PINKO_MOVE_AREA_RHS){
      speed = -speed;
    }
    if( pelletsFired > 0 ){
      for (int i = 0; i < pelletsFired; i++){
        pellets[i].move();
      }
    }
  } 

And the ActionPerformed method in JPanel class looks like:

public void actionPerformed(ActionEvent e){
    createLovely();
    if(numberOfLovelies > 0){
      for (int i = 0; i < numberOfLovelies; i++){
        lovelies[i].move();
      }
    }
    pinko.move();
    repaint();
  }

I have no idea why I keep getting the error mentioned above. Is there something wrong with the for loop in the move() method in Pinko class?? Any help will be much appreciated...

share|improve this question
5  
Where is line 75 of your class? Either area or pellets[i] is null. – assylias yesterday
@assylias I don't know which one is line 75, but I think it's pellets[i]? – Kimmm yesterday
1  
@Kimmm Change your IDE/software's settings to let you know the lines number, and it would be easy to fix. – ZouZou yesterday
The most interesting part of an exception is the line number: it helps you narrow down the problem to one line of code. You need to have easy access to line nunbers. – assylias yesterday

2 Answers

I would bet the NullPointerException happens here:

pellets[i].move();

Have you tried verifying that:

  • The Array is initialized
  • The index referenced contains an instance of what I suppose will be your Pellet class
share|improve this answer
I think I did initialise the array at the top of the Pinko class.. public static final int MAX_PELLETS = A2Constants.MAX_PELLETS; private Pellet[] pellets = new Pellet[MAX_PELLETS]; – Kimmm yesterday
Ok, so your array might be initialized, but it might not be populated. In short, are you sure that pellets[i] returns an instance of Pellet for every used value of i? – Mena yesterday

If you are using an IDE then try to use the debugger to help you understand what is going wrong in your code. Otherwise a few traces can help you debug and nail the problem : Here is the updated code you can try :

public void actionPerformed(ActionEvent e){ createLovely();

 if(numberOfLovelies > 0){
  for (int i = 0; i < numberOfLovelies; i++){
    if(lovelies[i] != null )
        lovelies[i].move();
    else
        System.out.println("ERROR: Null lovelies found at an index : " + i);
  }
}
if(pinko != null)
    pinko.move();
else {
   System.out.println("OOPS pinko is null");
}

repaint();

}

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.