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.