【问题标题】:LWJGL Application NullPointerException on mouseMoved鼠标移动上的 LWJGL 应用程序 NullPointerException
【发布时间】:2014-04-11 01:08:11
【问题描述】:

我第一次使用 LibGDX,在尝试在多路复用器中实现 InputAdapter 时遇到 NullPointerException。该消息似乎没有多大帮助,但我对我的 Java 相当生疏。我尝试使用实现 InputProcessor 接口的常规类,并且尝试记录任何会引发 Null 的内容,但我无法找到原因。

这是我的代码

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.g3d.Environment;
import com.badlogic.gdx.graphics.g3d.Material;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;

public class Game implements ApplicationListener {

    public PerspectiveCamera cam;
    public Model model;
    public ModelInstance instance;
    public ModelBatch modelBatch;
    public Environment environment;
    public InputMultiplexer input;

    public static int gWidth = 800;
    public static int gHeight = 600;

    @Override
    public void create() {

        this.modelBatch = new ModelBatch();

        this.environment = new Environment();
        this.environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
        this.environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));

        /**
         * Add input adapters.
         */
        this.cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        this.cam.position.set(10f, 20f, 0);
        this.cam.lookAt(0, 0, 0);
        this.cam.rotate(67f, 0f, 0f, 0f);
        this.cam.near = 1f;
        this.cam.far = 300f;        

        this.input = new InputMultiplexer(Gdx.input.getInputProcessor());
        this.input.addProcessor(new MouseCamController());
        Gdx.input.setInputProcessor(this.input);

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

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

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

//        if (this.cam.fieldOfView < 120) {
//            this.cam.fieldOfView += 10;
//        }
        this.cam.update();

        this.modelBatch.begin(this.cam);
        this.modelBatch.render(instance, environment);
        this.modelBatch.end();

    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
        this.modelBatch.dispose();
        this.model.dispose();
    }

    public class MouseCamController extends InputAdapter {
        @Override
        public boolean scrolled(int amount) {
//            Gdx.app.log("INPUT", "A: " + amount);
            return true;
        }
    }
}

以下是异常详情:

Exception in thread "LWJGL Application" java.lang.NullPointerException
    at com.badlogic.gdx.InputMultiplexer.mouseMoved(InputMultiplexer.java:107)
    at com.badlogic.gdx.backends.lwjgl.LwjglInput.processEvents(LwjglInput.java:303)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:200)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)

【问题讨论】:

    标签: java libgdx lwjgl


    【解决方案1】:

    问题在于获取当前的输入处理器。由于没有,当它开始通过每个适配器时,它实际上确实达到了 NULL。愚蠢的问题,简单的答案。问题已解决,可供后代使用。

    【讨论】:

      【解决方案2】:

      当 InputMultiplexer 到达其列表中的空 InputProcessor 时,就会出现问题。我尝试通过在返回的 InputProcessor 之后添加空处理器来复制它,并且不会发生错误。

      只需从 InputMultiplexer 中删除任何 null InputProcessor,它应该可以正常工作。如果您不确定哪一个为空,请尝试列出所有处理器。

      InputMultiplexer plexer;
      
      // Add processors to plexer...
      
      System.out.println(plexer.getProcessors().toString());
      

      【讨论】:

        猜你喜欢
        • 2015-02-01
        • 1970-01-01
        • 2023-03-31
        • 1970-01-01
        • 2014-06-27
        • 1970-01-01
        • 1970-01-01
        • 2012-12-25
        • 2015-12-08
        相关资源
        最近更新 更多