程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> java-程序報出NullPointerException

java-程序報出NullPointerException

編輯:編程綜合問答
程序報出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);

}
       ...
}

最佳回答:


肯定報空指針,因為首先你的cam沒有初始化,也沒有作為左值進行賦值。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved