简体   繁体   中英

I'm getting a NullPointerException in a java program

I've researched this quite a bit but I can't seem to find why I'm getting this error. I've ruled out the origin of the error coming from anywhere but "Transform .java:38", so I don't think I need to include that code. Line 38 is followed by 4 asterisks(*).

Error:

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)

Code:

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;
    }
}

More Code:

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;
    }

}

here's the answer: you don't need to research anything. you get a NullPointerException when you dereference a null reference. run your code in a debugger and figure out why the reference in question is null when you don't think it should be null. you need to learn how to debug these things yourself.

There is a typo in the constructor:

Wrong:

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

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

Correct:

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

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

There should have been a warning about the variable ps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM