我在Java中创建了一些类,似乎与此相关的是: Line3d,3d空间中的一行:
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;
import com.funguscow.obj.Line3d; import com.funguscow.obj.Point3d;
public class Cube扩展了Model {
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,包含两个ArrayLists,一个Model(Cube扩展)和一个Collider(CubeCollider释放):
package com.funguscow.model;
public class TestCube extends Complex{
public TestCube(){
super();
Cube c = (Cube) new Cube(100).position(100, 100, 40).setColor(0x00ffff);
c.init();
parts.add(c);
halts.add(new CubeCollider().setCube((Cube)c));
}
}
当我运行它时,我得到以下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)
我无法弄清楚为什么我会在那里得到一个NullPointerException,我不明白为什么会有任何未初始化的东西,但显然有一些问题。
答案 0 :(得分:0)
public Point3d a, b, c, d, e, f, g, h;
您没有初始化Point3d
班级的CubeCollider
成员,并且您尝试在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;
}
答案 1 :(得分:0)
当你看到你得到一个NullPointerException时,正确的做法是查看StackTrace(你的错误信息),它会准确地告诉你什么时候被调用。在这种情况下,导致错误的行是CubeCollider.setCube(CubeCollider.java:16)