NPE使用glfwWindowShouldClose(Kotlin)
所以我刚刚开始使用本教程的一个基本的LWJGL 3程序。 我已经把所有的代码转换成Kotlin来使它工作,一切似乎都很好。 直到我到了最后他利用glfwWindowShouldClose(window)
。 我按照他所展示的方式尝试了它,并使用自己的函数调用自己的方法来替换running
variables。 我甚至尝试用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
,它工作正常。 是否有可能是我的循环本身的实例导致问题,唯一的方法是不会抛出一个exception,如果循环不会发生( false
)?
您错过了一些重要的调用,比如GL.createCapabilities()
我强烈建议你从这里find的HelloWord开始
Ps:如果你使用kotlin,我有一个lib ,可以在几行中给你一个准备好的场景
with(glfw) { init() windowHint { context.version = "3.3"; profile = "core"; } } GlfwWindow(windowSize, title).apply { makeContextCurrent(); show(); } GL.createCapabilities()
- 处理程序()postDelay无法访问此Kotlin代码,如何解决它?
- 如何在Android Studio上设置Kotlin?
- 如何在Kotlin中创建一个固定大小的IntArray并初始化数组?
- 过时的Kotlin运行时间
- Android Kotlin中的Moshi – 将ENUM作为MutableMap键在非集成化时转换为字符串
- Kotlin:不能同时应用两个条件检查“return body”函数样式
- Kotlin:我怎样称呼super的扩展function?
- Haskell中的HasSupportFragmentInjector问题 – DispatchingAndroidInjector为null
- 通过Kotin使用OpenAM SDK时发现“没有类def”错误