NullPointerException,我不知道从哪里或为什么

NullPointerException, I can't tell from where or why

我在 Java 中做了一些 classes,似乎与此相关的是: Line3d,3d 中的一条线 space:

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

Cube,包含12个Line3d,组成一个立方体:

package com.funguscow.model;

导入com.funguscow.obj.Line3d; 导入 com.funguscow.obj.Point3d;

public class 多维数据集扩展模型{

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(抽象 class),如下所示:

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,它扩展了另一个抽象 class,Complex,包含两个 ArrayList,一个是 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,我不明白为什么会有任何未初始化的东西,但显然存在一些问题。

public Point3d a, b, c, d, e, f, g, h;

您没有初始化 CubeCollider class 的这些 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)