I have this:
public class Sprite
{
protected float x;
protected float y;
protected Image image;
protected Rectangle boundingBox;
public Rectangle getBoundingBox()
{
boundingBox = new Rectangle(x, y,
image.getWidth(), image.getHeight());
return boundingBox;
}
However I get a null pointer exception when I run it. The class sprite is never used by itself, only used as a superclass.
The only way for image to be defined is through the constructor:
Sprite(float x, float y, Image image) {
this.x = x;
this.y = y;
this.image = image;
}
Probably because the variable
imageis never initialized. Thereforeimage.getWidth()andimage.getHeight()will fail withNullPointerExceptions. Either initialize the variable. Or pass inimageor check fornullbefore you attempt to use it.