After compiling my application in Netbeans and running the application in Netbeans it works just fine.
All images load fine.
Trying to double click execute the application results in nothing happening.
Trying run from command line gives this error:
Exception in thread "main" java.lang.NullPointerException
at Entity.<init>(Entity.java:24)
at Actor.<init>(Actor.java:5)
at TileEngine.drawMap(TileEngine.java:52)
at GraphicsCanvas.<init>(GraphicsCanvas.java:32)
at Main.<init>(Main.java:22)
at Main.main(Main.java:18)
Compiling outside of Netbeans leaves no errors and execution is fine.
After trial and error of commenting I've came to these the anonymous call to Actor that is causing the problem. Here is function of the code that when commented out does not throw the exception. I cant seem to find anything wrong with it.
public class Actor extends Entity
{
Actor(String filename, int x, int y)
{
super(filename, x, y);
}
}
void drawMap(String imgWalkable, String imgNotWalkable, GraphicsCanvas gp)
{
// Since each 1 tile is a tile that can be walked on
// we need to set the 1 tile to something you can walk on.
// The tiles that cannot be walked on are the ones that are 0
for (int x = 0; x < WID; x++)
{
for (int y = 0; y < HEI; y++)
{
if (GRID[x][y] == 1)
gp.add(new Actor(imgWalkable, x * TILE_WID, y * TILE_HEI));
//else
//gp.add(new Actor(imgNotWalkable, x * TILE_WID, y * TILE_HEI));
}
}
}
I have further traced this error to BufferedImage in my Entity
class.
public class Entity extends JLabel
{
Entity(String filename, int x, int y)
{
this.x = x;
this.y = y;
this.setLocation(this.x, this.y);
bImg = loadImage(filename);
this.setSize(bImg.getWidth(), bImg.getHeight());
ImageIcon icon = new ImageIcon(bImg);
setIcon(icon);
}
public BufferedImage loadImage(String filename) {
BufferedImage tmp = null;
try {
tmp = ImageIO.read(getClass().getResource(filename));
} catch (Exception e) { }
return tmp;
}
}
After removing loadImage function and instead loading the image like this:
Entity(String filename, int x, int y)
{
try {
bImg = ImageIO.read(getClass().getResource(filename)); //LINE 25
} catch (IOException ex) {
Logger.getLogger(Entity.class.getName()).log(Level.SEVERE, null, ex);
}
this.x = x;
this.y = y;
this.setLocation(this.x, this.y);
//bImg = loadImage(filename);
//loadImage(bImg, filename);
this.setSize(bImg.getWidth(), bImg.getHeight());
Icon icon = new ImageIcon(bImg);
setIcon(icon);
setVisible(isAlive);
}
This new error is received:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1362)
at Entity.<init>(Entity.java:25)