2 o19830015 o19830015 于 2013.06.03 11:43 提问

程序报出NullPointerException

在第39行代码:modelBatch.begin(cam);报出 NullPointerException 异常。

请帮忙看看怎么回事,谢谢。

public class Loading implements Screen {

private boolean AP;
private Chemistry chemistry;
public PerspectiveCamera cam;
public ModelBatch modelBatch;
public Model model;
public ModelInstance instance;
public Lights lights;

public Loading(boolean AP, Chemistry chemistry) {
    this.AP = AP;
    this.chemistry = chemistry;
}

@Override
public void render(float delta) {
    Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(),
            Gdx.graphics.getHeight());
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    modelBatch.begin(cam);
    modelBatch.render(instance, lights);
    modelBatch.end();
}

@Override
public void resize(int width, int height) {
}

@Override
public void show() {
            modelBatch = new ModelBatch();
    lights = new Lights();
    lights.ambientLight.set(0.4f, 0.4f, 0.4f, 1f);
    lights.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f,
            -0.2f));

    cam = new PerspectiveCamera(70, Gdx.graphics.getWidth(),
            Gdx.graphics.getHeight());
    cam.position.set(10f, 10f, 10f);
    cam.lookAt(0, 0, 0);
    cam.near = 0.1f;
    cam.far = 300f;
    cam.update();

    ModelBuilder modelBuilder = new ModelBuilder();
    model = modelBuilder.createBox(5f, 5f, 5f,
            new Material(ColorAttribute.createDiffuse(Color.GREEN)),
            Usage.Position | Usage.Normal);

    instance = new ModelInstance(model);

}
       ...
}

1个回答

u010122579
u010122579   2013.06.03 11:46
已采纳

肯定报空指针,因为首先你的cam没有初始化,也没有作为左值进行赋值。

Csdn user default icon
上传中...
上传图片
插入图片