为什么这会导致空指针异常?

Der*_*rek 0 java initialization

我有这个代码:

public class Sprite {
    protected float x;
    protected float y;
    protected Image image;
    protected Rectangle boundingBox;

    Sprite(float x, float y, Image image) {
        this.x = x;
        this.y = y;
        this.image = image;
        boundingBox = new Rectangle(x, y, 
            this.image.getWidth(), this.image.getHeight());
    }
Run Code Online (Sandbox Code Playgroud)

...

public Rectangle getBoundingBox() {
    return boundingBox;
}
Run Code Online (Sandbox Code Playgroud)

但是,一旦定义并初始化了sprite对象,我在另一个类中调用此函数时:

public static boolean collides(Sprite object1, Sprite object2) {
    return object1.getBoundingBox().intersects(object2.getBoundingBox());
}
Run Code Online (Sandbox Code Playgroud)

我得到一个空指针异常,指向包含这个的行:

this.image.getWidth(), this.image.getHeight());
Run Code Online (Sandbox Code Playgroud)

为什么是这样?

Nic*_*las 5

检查图像是否为空.这很可能是错误发生的地方.