Java NullPointerException,我可以';我不知道从哪里或者为什么
我用Java编写了几个类,这些类似乎与此相关: Line3d,三维空间中的线:Java NullPointerException,我可以';我不知道从哪里或者为什么,java,3d,nullpointerexception,null,runtime-error,Java,3d,Nullpointerexception,Null,Runtime Error,我用Java编写了几个类,这些类似乎与此相关: Line3d,三维空间中的线: package com.funguscow.obj; public class Line3d { public Point3d start, end; public Line3d(int a, int b, int c, int x, int y, int z){ start = new Point3d(a, b, c); end = new Point3d(x,
package com.funguscow.obj;
public class Line3d {
public Point3d start, end;
public Line3d(int a, int b, int c, int x, int y, int z){
start = new Point3d(a, b, c);
end = new Point3d(x, y, z);
}
public Line3d(Point3d s, Point3d e){
start = s;
end = e;
}
public Line3d scale(float f){
start.x *= f;
start.y *= f;
start.z *= f;
end.x *= f;
end.y *= f;
end.z *= f;
return this;
}
public Line3d translate(float x, float y, float z){
return translate(new Point3d(x, y, z));
}
public Line3d translate(Point3d p){
start.add(p);
end.add(p);
return this;
}
}
立方体,包含12个Line3d,形成立方体:
package com.funguscow.model;
导入com.funguscow.obj.Line3d;
导入com.funguscow.obj.Point3d
公共类多维数据集扩展模型{
public Cube(float s){
super(s);
init();
}
public void init(){
super.init();
lines.clear();
Point3d a = new Point3d(scale, scale, scale).add(position),
b = new Point3d(scale, scale, -scale).add(position),
c = new Point3d(scale, -scale, scale).add(position),
d = new Point3d(scale, -scale, -scale).add(position),
e = new Point3d(-scale, scale, scale).add(position),
f = new Point3d(-scale, scale, -scale).add(position),
g = new Point3d(-scale, -scale, scale).add(position),
h = new Point3d(-scale, -scale, -scale).add(position);
addLine(a, b);
addLine(a, c);
addLine(a, e);
addLine(b, d);
addLine(b, f);
addLine(c, d);
addLine(c, g);
addLine(d, h);
addLine(e, f);
addLine(e, g);
addLine(f, h);
addLine(g, h);
}
public Point3d getPosition(){
return Point3d.average(lines.get(0).start, lines.get(11).end);
}
}
CubeCollider,扩展了Collider(一个抽象类),如下所示:
package com.funguscow.model;
import com.funguscow.obj.Point3d;
import com.funguscow.obj.Vector3d;
public class CubeCollider extends Collider{
public Point3d a, b, c, d, e, f, g, h;
public CubeCollider setCube(Cube cube){
return setCube((float)cube.lines.get(0).start.x,
(float)cube.lines.get(0).start.y, (float)cube.lines.get(0).start.z,
(float)cube.lines.get(11).end.x, (float)cube.lines.get(11).end.y, (float)cube.lines.get(11).end.z);
}
public CubeCollider setCube(float x, float y, float z, float m, float n, float o){
a.x = b.x = c.x = d.x = x;
e.x = f.x = g.x = h.x = m;
a.y = c.y = e.y = g.y = y;
b.y = d.y = f.y = h.y = n;
a.z = b.z = e.z = f.z = z;
c.z = d.z = g.z = h.z = o;
return this;
}
}
TestCube扩展了另一个抽象类Complex,它包含两个ArrayList,一个是Model(多维数据集扩展的),另一个是Collider(CubeCollider扩展的):
运行它时,我得到以下NullPointerException,指向CubeCollider中的SetCube函数:
Exception in thread "main" java.lang.NullPointerException
at com.funguscow.model.CubeCollider.setCube(CubeCollider.java:16)
at com.funguscow.model.CubeCollider.setCube(CubeCollider.java:10)
at com.funguscow.model.TestCube.<init>(TestCube.java:10)
at com.funguscow.world.World.init(World.java:33)
at com.funguscow.world.World.<init>(World.java:24)
at com.funguscow.game.Game.<init>(Game.java:40)
at com.funguscow.game.Main.main(Main.java:20)
线程“main”java.lang.NullPointerException中的异常
位于com.funguscow.model.CubeCollider.setCube(CubeCollider.java:16)
位于com.funguscow.model.CubeCollider.setCube(CubeCollider.java:10)
位于com.funguscow.model.TestCube(TestCube.java:10)
位于com.funguscow.world.world.init(world.java:33)
在com.funguscow.world.world.(world.java:24)
在com.funguscow.game.game.(game.java:40)
位于com.funguscow.game.Main.Main(Main.java:20)
我不明白为什么我会得到一个NullPointerException,我不明白为什么会有任何未初始化的东西,但显然存在一些问题
public Point3d a, b, c, d, e, f, g, h;
您没有初始化CubeCollider
类的Point3d
成员,您试图在setCube
中访问它们,导致NullPointerException
这应该起作用:
public CubeCollider setCube(float x, float y, float z, float m, float n, float o){
a = new Point3D(x,y,z);
b = new Point3D(x,n,z);
c = new Point3D(x,y,o);
...
return this;
}
当您看到收到一个NullPointerException时,正确的做法是查看StackTrace(您的错误消息),它将准确地告诉您何时调用了什么。在本例中,导致错误的行是CubeCollider.setCube(CubeCollider.java:16)
public CubeCollider setCube(float x, float y, float z, float m, float n, float o){
a = new Point3D(x,y,z);
b = new Point3D(x,n,z);
c = new Point3D(x,y,o);
...
return this;
}