Null object calling error stuff :S [SOLVED]

Hi guys, I am a noob learning about Java game dev and I decided to start of simple with a basic Java2D scrolling shooter. I’ve been looking at various tutorials and managed to make a basic ship that can move around. I then went on to creating missiles that i can shoot, but when i finished writing the code and I ran the program i keep on getting the same error:
[spoiler]

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at galaxyrunner.Board.actionPerformed(Board.java:37)
	at javax.swing.Timer.fireActionPerformed(Timer.java:291)
	at javax.swing.Timer$DoPostEvent.run(Timer.java:221)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:682)
	at java.awt.EventQueue.access$000(EventQueue.java:85)
	at java.awt.EventQueue$1.run(EventQueue.java:643)
	at java.awt.EventQueue$1.run(EventQueue.java:641)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:652)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at galaxyrunner.Board.paint(Board.java:86)
	at javax.swing.JComponent.paintChildren(JComponent.java:862)
	at javax.swing.JComponent.paint(JComponent.java:1038)
	at javax.swing.JComponent.paintChildren(JComponent.java:862)
	at javax.swing.JComponent.paint(JComponent.java:1038)
	at javax.swing.JLayeredPane.paint(JLayeredPane.java:567)
	at javax.swing.JComponent.paintChildren(JComponent.java:862)
	at javax.swing.JComponent.paint(JComponent.java:1038)
	at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:34)
	at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
	at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
	at java.awt.Container.paint(Container.java:1791)
	at java.awt.Window.paint(Window.java:3379)
	at sun.awt.RepaintArea.paintComponent(RepaintArea.java:276)
	at sun.awt.RepaintArea.paint(RepaintArea.java:241)
	at apple.awt.ComponentModel.handleEvent(ComponentModel.java:263)
	at apple.awt.CWindow.handleEvent(CWindow.java:545)
	at java.awt.Component.dispatchEventImpl(Component.java:4813)
	at java.awt.Container.dispatchEventImpl(Container.java:2141)
	at java.awt.Window.dispatchEventImpl(Window.java:2482)
	at java.awt.Component.dispatchEvent(Component.java:4565)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:684)
	at java.awt.EventQueue.access$000(EventQueue.java:85)
	at java.awt.EventQueue$1.run(EventQueue.java:643)
	at java.awt.EventQueue$1.run(EventQueue.java:641)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
	at java.awt.EventQueue$2.run(EventQueue.java:657)
	at java.awt.EventQueue$2.run(EventQueue.java:655)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:654)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

[/spoiler]
From what i know this means i am calling a metjod of a null object that shouldn’t be null. Please correct me if im wrong, which i probably am. I would love to know whats going on and how i can fix this.

Here is the code for Board.java:
[spoiler]

//@author KMeister Copyright © 2012

package galaxyrunner;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;

public class Board extends JPanel implements ActionListener
{
    public int score;
    private Ship ship;
    private Image stars;
    private Image pauseMenu;
    private Timer timer;
    private int gameSpeed;
    
    public Board() 
    {
        addKeyListener(new ActionListener());
        setBackground(Color.BLACK);
        ship = new Ship();
        gameSpeed = 20;
        setFocusable(true);
        ImageIcon imageicon = new ImageIcon("/Users/kids/NetBeansProjects/GalaxyRunner/src/galaxyrunner/PNG/stars.png");
        stars = imageicon.getImage();
        ImageIcon imageicon2 = new ImageIcon("/Users/kids/NetBeansProjects/GalaxyRunner/src/galaxyrunner/PNG/pause_menu.png");
        pauseMenu = imageicon2.getImage();
        timer = new Timer(gameSpeed, this);
        timer.start();
    }

    public void actionPerformed(ActionEvent actionevent) 
    {   
        ArrayList missiles = ship.getMissiles();
        for(int i = 0; i < missiles.size(); i++)
        {
            Missile missile = (Missile) missiles.get(i);
            if(missile.getVisible() == true)
            {
                missile.move();
            }else
            {
                missiles.remove(i);
            } 
        }
        
        ship.move();
        repaint();
        score = (ship.distanceBackground + ship.distanceShip) / 10;
    }
    
    public void paint(Graphics graphics)
    {
        super.paint(graphics);
        Graphics2D graphics2d = (Graphics2D)graphics;
        
        graphics2d.drawImage(stars, 0, -ship.backgroundPosY - 2300, this);  
        
        graphics2d.drawImage(ship.getImage(), ship.getPosX(), 550, this);
        
        if(ship.isPaused == true)
        {
            graphics2d.drawImage(pauseMenu, 0, 0, this);
            
            Font small = new Font("Futura", Font.BOLD, 14);
            FontMetrics metr = this.getFontMetrics(small);

            graphics2d.setColor(Color.white);
            graphics2d.setFont(small);
            graphics2d.drawString("Your Score: " + score, 138, 550);
        }
        if(ship.isPaused == false)
        {
            Font small = new Font("Futura", Font.BOLD, 14);
            FontMetrics metr = this.getFontMetrics(small);

            graphics2d.setColor(Color.white);
            graphics2d.setFont(small);
            graphics2d.drawString("Your Score: " + score, 5, 19);
            graphics2d.drawString("High Score: " + "WIP", 240, 19);
        }
        
        ArrayList missiles = ship.getMissiles();
        for(int i = 0; i < missiles.size(); i++)
        {
            Missile missile = (Missile) missiles.get(i);
            graphics2d.drawImage(missile.getImage(), missile.posX, missile.posY, this);
        }
    }
    
    private class ActionListener extends KeyAdapter
    {
        public void keyPressed(KeyEvent event)
        {          
                ship.keyPressed(event);
        }
        
        public void keyReleased(KeyEvent event)
        {
            ship.keyReleased(event);
        }
    }  
}

[/spoiler]

Also can i put code in a spoiler so as to make the post a bit shorter and more manageable?
Thanks in advance :slight_smile:
-KMeister

Code should go in code tags like you did. Anyway, a NPE should never be mysterious as long as you have access to the code. The second line of the traceback tells you where the error occurred:


for(int i = 0; i < missiles.size(); i++)

There is only one thing that could be null here, and that’s missiles, which you get on the previous line:


ArrayList missiles = ship.getMissiles();

Clearly you’re not initializing the missiles field in your ship object. Also, not related to this, but your IDE should be screaming at you about how you’re using a raw type instead of a generic. Pay attention to it – you should not be using ArrayList by itself without a generic type. See http://docs.oracle.com/javase/tutorial/java/generics/ for more info there.

IMHO, if the guy is having problems with a NPE with a full stack trace, generics is something that he definetely doesn’t need to worry right now.

Generics is still advisable even in this situation with such a simple NPE.

The OP needs to learn how to properly debug given that the exception gave him the line number where the exception occurred :wink:

I’m not saying generics are not advisable. I’m just saying it is not advisable to worry about it now. He clearly has more important things to learn first.

Being new is not a reason to use code that’s wrong, and using a raw type when you don’t absolutely have to is wrong. One can use an array if they don’t want to deal with the topic, but simple one-parameter non-wildcarded generics are not hard at all. In fact the feedback you get from the compiler should make things easier.

You still don’t get what I’m saying.

When you are learning a new language, let’s say French, do you first learn to say “Good morning, Good night”, or you learn all the correct prepositions and verb conjugations? The first step is to be understood (or make the code run). After you’ve learned to ask for directions and say “thank you” in french (that is, deal with basic null pointer exceptions), then I think you should worry about prepositions and correct verb conjugation (or Generics.).

As I said from the first post:
“He clearly has more important things to learn first.”
Making him worry about generics or annotations or whatever before having a firm grasp of what might cause a null pointer, in my opinion is totally useless.
Think about it,if he couldn’t see what the problem was in this snippet, do you really think he’ll understand why Generics are useful?

Sorry I brought it up, I guess we’re just going to have to agree to disagree, if we’re even doing that.

teletubo – Look at his code; he clearly understands enough Java (if statements, for loops, Java2D) to start learning “proper” coding standards.

Not only is the following “better” but (I would argue) it’s also clearer to a newbie.
e.g.

List<Blah> list = new ArrayList<Blah>();
for (Blah b : list)
    ... do something ...

Using generics like that is not a complicated thing; it’s probably more complicated to explain to a newbie why a cast is required.

ok, then I agree to disagree.

I’m with teletubo on this one. His code suffers from a lot of issues, but at least it works with some trivial fixes.

Have you seen his loop-body? How are you going to explain the ConcurrentModificationException ?

Yes, the problem is caused by his ‘remove-during-iteration’ code, but as he’s struggling with nullpointers, I prefer to take 1 step back, not pointing out all issues, because he’ll be flooded and probably losing motivation. Nothing as disheartening as making a bunch of changes ‘for the better’, introducing lots of new problems.

I thought i had already initialized but i forgot to add this line in:

missiles = new ArrayList();

now it works \o/ ;D

I will also look into the generics thing :slight_smile:

Thanks for the help!

-KMeister