简体   繁体   中英

NullPointerException, I can't tell from where or why

I've made a few classes in Java, the ones that seem to relate to this are: Line3d, a line in 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, which contains 12 Line3d's, forming a cube:

package com.funguscow.model;

import com.funguscow.obj.Line3d; import com.funguscow.obj.Point3d;

public class Cube extends 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, extending Collider(an abstract class), that looks like the following:

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

And TestCube, which extends another abstract class, Complex, containing two ArrayLists, one of Model(which Cube extends), and one of Collider(which CubeCollider exends):

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

}

When I run it I get the following NullPointerException, pointing to the SetCube function in CubeCollider:

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)

I can't figure out why on earth I'd be getting a NullPointerException there, I can't see why there'd be anything that's not initialized, but there's clearly some problem.

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

You don't initialize these Point3d members of the CubeCollider class, and you try to access them in setCube , causing NullPointerException .

This should work :

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

When you see that you are getting a NullPointerException, the right thing to do is to look at the StackTrace (Your error message) and it will tell exactly what got called when. In this case, the line that caused the error was CubeCollider.setCube(CubeCollider.java:16)

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