我在java程序中得到一个NullPointerException

时间:2013-10-21 23:49:02

标签: java nullpointerexception

我对此进行了相当多的研究,但我似乎无法找到为什么我会收到此错误。我已经排除了来自“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;
    }

}

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分: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的警告。