0

I am creating a game in which I import images from files to represent characters, etc. I have created a class called Contents which represents anything that can be placed on a tile of the gameboard. A subclass of Contents (by several levels) is Wizard, to create a wizard character. I have it set up so that Contents contains several protected variables whose values are set in Wizard.

protected Tile currentTile; //The tile of the contents
protected final Toolkit tools=Toolkit.getDefaultToolkit();
protected URL url; //url of the image corresponding to the contents
protected Image image; //the image itself

In Wizard,

url=Wizard.class.getResource("Wizard.PNG");
image=tools.getImage(url);

Contents also contains a draw method to draw it on a specified tile.

public void draw(Graphics g, ImageObserver i)
{
    try
    {
        g.drawImage(image, (currentTile.getX()*30)+10, (currentTile.getY()*30)+10, 29, 29, i); //EXCEPTION
    }
    catch(NullPointerException e)
    {
        e.printStackTrace();
    }
}

I get a NullPointerException on the line where I try to draw the image. However, when I go to Wizard and print out both the URL and the image, both come out as expected. Not sure why the image is seen as being null. Any help would be awesome.

3
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by pi_lord12
0

What object reference is null causing the NPE?
On that line there are three object references. Which one is null?

0

Wow... I feel like an idiot. Instead of using a method to set currentTile, I used the method to return the tile that I wanted to set it to... thanks and sorry about that!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.