gpt4 book ai didi

java - 我在 java 程序中遇到 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 23:29:46 25 4
gpt4 key购买 nike

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

}

最佳答案

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

关于java - 我在 java 程序中遇到 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19506728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com