gpt4 book ai didi

java - GL.createCapability 抛出 NullPointerException

转载 作者:行者123 更新时间:2023-11-30 07:18:10 26 4
gpt4 key购买 nike

请注意关闭扣动扳机的手指的选民——这不是“什么是 NullPointerException”的重复。在投票结束之前阅读整个问题。

<小时/>

我使用 LWJGL3 库创建了一个用于制作窗口的类。我正在尝试使用此类在单元测试中创建一个窗口,但它在 GL.createCapability() 方法调用上引发 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();
}

}

最佳答案

我遇到此错误是因为我忘记初始化 GLFW。很奇怪,因为我在它工作之前调用了一些 GLFW 函数。

关于java - GL.createCapability 抛出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38041130/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com