GL.createCapabilities抛出NullPointerException

时间:2016-06-26 17:19:59

标签: java nullpointerexception lwjgl

注意用痒的触发手指关闭选民 - 这不是"什么是NullPointerException"。在投票结束前阅读整个问题。

我使用LWJGL3库创建了一个用于创建窗口的类。 我尝试使用此类在单元测试中创建一个窗口,但它会在GL.createCapabilities()方法调用上抛出NullPointerException。我使用Intellij IDEA和gradle,以及Debian 8。

这不是什么是NullPointerException的重复,因为我试图找出LWJGL3库抛出此异常的原因,并且我知道这个异常是什么。

我已经在Google上搜索过了,我也没有找到为什么会这样。我尝试删除libgl1-mesa-dev包,但它没有用。

这是堆栈跟踪。

java.lang.NullPointerException
        at java.util.regex.Matcher.getTextLength(Matcher.java:1283)
        at java.util.regex.Matcher.reset(Matcher.java:309)
        at java.util.regex.Matcher.<init>(Matcher.java:229)
        at java.util.regex.Pattern.matcher(Pattern.java:1093)
        at org.lwjgl.system.APIUtil.apiParseVersion(APIUtil.java:140)
        at org.lwjgl.system.APIUtil.apiParseVersion(APIUtil.java:122)
        at org.lwjgl.opengl.GL.createCapabilities(GL.java:278)
        at org.lwjgl.opengl.GL.createCapabilities(GL.java:226)
        at com.advancid.minage.gui.Window.makeCurrentContext(Window.java:85)
        at com.advancid.minage.gui.Window.<init>(Window.java:19)
        at com.advancid.minage.gui.WindowTest.workForOneWindow(WindowTest.java:9)

这是我的单元测试课程。

package com.advancid.minage.gui;

import org.junit.Test;

public class WindowTest {

    @Test
    public void workForOneWindow() {
        Window window = new Window("My window", 1280, 720);
        window.show();
        window.makeCurrentContext();
        window.update();
        window.destroy();
    }

}

这是我的窗口类。

package com.advancid.minage.gui;

import org.lwjgl.BufferUtils;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GLCapabilities;
import org.lwjgl.system.MemoryUtil;

import java.nio.IntBuffer;

public class Window {

    private long handle;
    private GLCapabilities capabilities = null;

    public Window(String title, int width, int height) {
        GLFW.glfwDefaultWindowHints();
        this.handle = GLFW.glfwCreateWindow(width, height, title, MemoryUtil.NULL, MemoryUtil.NULL);
        this.makeCurrentContext();
    }

    public void show() {
        GLFW.glfwShowWindow(this.handle);
    }

    public void hide() {
        GLFW.glfwHideWindow(this.handle);
    }

    public boolean shouldClose() {
        return GLFW.glfwWindowShouldClose(this.handle) == GLFW.GLFW_TRUE;
    }

    public void resize(int width, int height) {
        GLFW.glfwSetWindowSize(this.handle, width, height);
    }

    public int[] getSize() {
        IntBuffer widthBuffer = BufferUtils.createIntBuffer(1);
        IntBuffer heightBuffer = BufferUtils.createIntBuffer(1);

        GLFW.glfwGetWindowSize(this.handle, widthBuffer, heightBuffer);

        return new int[] { widthBuffer.get(), heightBuffer.get() };
    }

    public int getWidth() {
        return getSize()[0];
    }

    public int getHeight() {
        return getSize()[1];
    }

    public void setPosition(int x, int y) {
        GLFW.glfwSetWindowPos(this.handle, x, y);
    }

    public int[] getPosition() {
        IntBuffer xBuffer = BufferUtils.createIntBuffer(1);
        IntBuffer yBuffer = BufferUtils.createIntBuffer(1);

        GLFW.glfwGetWindowPos(this.handle, xBuffer, yBuffer);

        return new int[] { xBuffer.get(), yBuffer.get() };
    }

    public int getX() {
        return getPosition()[0];
    }

    public int getY() {
        return getPosition()[1];
    }

    public void destroy() {
        GLFW.glfwDestroyWindow(this.handle);
    }

    public void makeCurrentContext() {
        GLFW.glfwMakeContextCurrent(this.handle);
        GLFW.glfwSwapInterval(1);

        if (this.capabilities == null) {
            this.capabilities = GL.createCapabilities();
        } else {
            GL.setCapabilities(this.capabilities);
        }
    }

    public void update() {
        GLFW.glfwSwapBuffers(this.handle);
        GLFW.glfwPollEvents();
    }

}

1 个答案:

答案 0 :(得分:1)

我遇到了这个错误,因为我忘了初始化GLFW。 非常奇怪,因为我在它工作之前调用了一些GLFW函数。