【发布时间】:2018-07-16 00:35:57
【问题描述】:
所以我刚刚开始使用这个tutorial 编写一个基本的 LWJGL 3 程序。我已将所有代码转换为 Kotlin 以使其工作,一切似乎都很好。直到我走到最后,他使用了glfwWindowShouldClose(window)。我尝试了他展示的方式,以及我自己用函数调用本身替换running 变量的方法。我什至尝试用true 替换它。不幸的是,它似乎不起作用。
澄清一下,我的意思是当我在项目中的任何地方使用glfwWindowShouldClose(window) 时,对 LWJGL 函数的任何调用都会导致 NPE,即使是与它无关的函数:
Exception in thread "thingy" java.lang.NullPointerException
at org.lwjgl.system.Checks.check(Checks.java:98)
at org.lwjgl.glfw.GLFW.glfwSwapBuffers(GLFW.java:4206)
at main.Window.render(main.kt:39)
at main.Window.run(main.kt:15)
at java.lang.Thread.run(Thread.java:745)
我用于这个错误示例的代码在这里:
class Window: Runnable {
private val thread = Thread(this, "thingy")
private val window: Long
override fun run() {
while (true) {
update()
render()
}
}
init { thread.start(); window = init() }
private fun init(): Long {
if (!glfwInit()) System.err.println("Couldn't initialize GLFW.")
glfwWindowHint(GLFW_RESIZABLE, 1)
val window = glfwCreateWindow(800, 600, "thingy", NULL, NULL)
if (window == NULL) System.err.println("Couldn't create a window.")
val vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor())
glfwSetWindowPos(window, 100, 100)
glfwMakeContextCurrent(window)
glfwShowWindow(window)
return window
}
private fun update() { glfwPollEvents() }
private fun render() { glfwSwapBuffers(window) }
}
如果我删除函数调用并在 while 语句中将其替换为 false,它可以正常工作。我的循环实例本身是否可能导致问题,并且它不引发异常的唯一方法是循环永远不会发生(false)?
【问题讨论】:
标签: nullpointerexception kotlin lwjgl