user6377043
user6377043

Reputation:

GL.createCapabilities throws NullPointerException

Note to close voters with itchy trigger fingers -- This is NOT a dup of "What is a NullPointerException". Read the entire question before voting to close.


I made a class for making window using the LWJGL3 library. I'm trying to create a window in a unit test with this class, but it throws a NullPointerException on the GL.createCapabilities() method call. I'm using Intellij IDEA with gradle, and Debian 8.

This is not a duplicate of what is a NullPointerException since I'm trying to find the reason why the LWJGL3 library throws this exception, and I know what is this exception.

I have already searched on Google, and I didn't found why this could happen. I tried removing the libgl1-mesa-dev package and it didn't work.

Here is the stack trace.

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)

Here is my unit test class.

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

}

And here is my window class.

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

}

Upvotes: 1

Views: 421

Answers (1)

user6377043
user6377043

Reputation:

I had this error because I forgot to initialize GLFW. Very strange because I called some GLFW functions before it worked.

Upvotes: 1

Related Questions