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.

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

Four, if you're counting the Graphics context.

I can only see three: g, image and currentTile

i is an ImageObserver, isn't it?

Yep. My glasses need cleaning. Damn single letter variables.

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.