1

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

}
  • Can you please add Stack trace of exception? – PVR Jun 26 '16 at 17:21
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Tom Jun 26 '16 at 17:27
  • 3
    @Tom This is not a duplicate of that question since it is a third party library which throws this exception, and I know what is a NullPointerException –  Jun 26 '16 at 17:34
  • @Advancid You should ALWAYS include in your post references you have already followed up on the Internet. It prevents people from wasting time repeating the same research. Please get into the habit of doing that on EVERY question you ask. – Jim Garrison Jun 26 '16 at 17:47

1 Answers1

1

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