0

我已经对此进行了很多研究,但似乎无法找到为什么会出现此错误。我已经排除了错误的来源,除了“Transform .java:38”之外的任何地方,所以我认为我不需要包含该代码。第 38 行后跟 4 个星号 (*)。

错误:

Exception in thread "main" java.lang.NullPointerException
    at com.base.engine.Transform.getProjectedTransformation(Transform.java:38)
    at com.base.engine.Game.render(Game.java:67)
    at com.base.engine.MainComponent.render(MainComponent.java:111)
    at com.base.engine.MainComponent.run(MainComponent.java:102)
    at com.base.engine.MainComponent.start(MainComponent.java:28)
    at com.base.engine.MainComponent.main(MainComponent.java:126)

代码:

package com.base.engine;

public class Transform
    {
    private static Camera camera;

    private static float zNear;
    private static float zFar;
    private static float width;
    private static float height;
    private static float fov;

    private Vector3f translation;
    private Vector3f rotation;
    private Vector3f scale;

    public Transform()
    {
        translation = new Vector3f(0,0,0);
        rotation = new Vector3f(0,0,0);
        scale = new Vector3f(1,1,1);
    }

    public Matrix4f getTransformation()
    {
        Matrix4f translationMatrix = new Matrix4f().initTranslation(translation.getX(), translation.getY(), translation.getZ());
        Matrix4f rotationMatrix = new Matrix4f().initRotation(rotation.getX(), rotation.getY(), rotation.getZ());
        Matrix4f scaleMatrix = new Matrix4f().initScale(scale.getX(), scale.getY(), scale.getZ());

        return translationMatrix.mul(rotationMatrix.mul(scaleMatrix));
    }

    public Matrix4f getProjectedTransformation()
    {
        Matrix4f transformationMatrix = getTransformation();
        Matrix4f projectionMatrix = new Matrix4f().initProjection(fov, width, height, zNear, zFar);
        Matrix4f cameraRotation = new Matrix4f().initCamera(camera.getForward(), camera.getUp());
        ****Matrix4f cameraTranslation = new Matrix4f().initTranslation(-camera.getPos().getX(), -camera.getPos().getY(), -camera.getPos().getZ());

        return projectionMatrix.mul(cameraRotation.mul(cameraTranslation.mul(transformationMatrix)));
    }


    public Vector3f getTranslation()
    {
        return translation;
    }

    public static void setProjection(float fov, float width, float height, float zNear, float zFar)
    {
        Transform.fov = fov;
        Transform.width = width;
        Transform.height = height;
        Transform.zNear = zNear;
        Transform.zFar = zFar;
    }

    public void setTranslation(Vector3f translation)
    {
        this.translation = translation;
    }

    public void setTranslation(float x, float y, float z)
    {
        this.translation = new Vector3f(x, y, z);
    }

    public Vector3f getRotation()
    {
        return rotation;
    }

    public void setRotation(Vector3f rotation)
    {
        this.rotation = rotation;
    }

    public void setRotation(float x, float y, float z)
    {
        this.rotation = new Vector3f(x, y, z);
    }

    public Vector3f getScale()
    {
        return scale;
    }

    public void setScale(Vector3f scale)
    {
        this.scale = scale;
    }

    public void setScale(float x, float y, float z)
    {
        this.scale = new Vector3f(x, y, z);
    }

    public static Camera getCamera()
    {
        return camera;
    }

    public static void setCamera(Camera camera)
    {
        Transform.camera = camera;
    }
}

更多代码:

package com.base.engine;

public class Camera 
{
    public static final Vector3f yAxis = new Vector3f(0,1,0);

    private Vector3f pos;
    private Vector3f forward;
    private Vector3f up;

    public Camera()
    {
        this(new Vector3f(0,0,0), new Vector3f(0,0,1), new Vector3f(0,1,0));
    }

    public Camera(Vector3f ps, Vector3f forward, Vector3f up)
    {
        this.pos = pos;
        this.forward = forward;
        this.up = up;

        up.normalize();
        forward.normalize();
    }

    public void move(Vector3f dir, float amt)
    {
        pos = pos.add(dir.mul(amt));
    }

    public void rotateY(float angle)
    {
        Vector3f Haxis = yAxis.cross(forward);
        Haxis.normalize();

        forward.rotate(angle, yAxis);
        forward.normalize();

        up = forward.cross(Haxis);
        up.normalize();
    }

    public void rotateX(float angle)
    {
        Vector3f Haxis = yAxis.cross(forward);
        Haxis.normalize();

        forward.rotate(angle, Haxis);
        forward.normalize();

        up = forward.cross(Haxis);
        up.normalize();
    }

    public Vector3f getLeft()
    {
        Vector3f left = up.cross(forward);
        left.normalize();
        return left;
    }

    public Vector3f getRight()
    {
        Vector3f right = forward.cross(up);
        right.normalize();
        return right; 
    }

    public Vector3f getPos() {
        return pos;
    }

    public void setPos(Vector3f pos) {
        this.pos = pos;
    }

    public Vector3f getForward() {
        return forward;
    }

    public void setForward(Vector3f forward) {
        this.forward = forward;
    }

    public Vector3f getUp() {
        return up;
    }

    public void setUp(Vector3f up) {
        this.up = up;
    }

}
4

2 回答 2

2

答案是:你不需要研究任何东西。当您取消引用空引用时,您会收到 NullPointerException。在调试器中运行您的代码,并在您认为相关引用不应该为空时找出为什么有问题的引用为空。您需要学习如何自己调试这些东西。

于 2013-10-21T23:57:17.710 回答
1

构造函数中有一个错字:

错误的:

public Camera(Vector3f ps, Vector3f forward, Vector3f up)
{
    this.pos = pos;
    this.forward = forward;
    this.up = up;

    up.normalize();
    forward.normalize();
}

正确的:

public Camera(Vector3f pos, Vector3f forward, Vector3f up)
{
    this.pos = pos;
    this.forward = forward;
    this.up = up;

    up.normalize();
    forward.normalize();
}

应该有关于变量 ps 的警告。

于 2013-10-22T00:01:44.957 回答