Jump to content

Java LWJGL 3 Exception

Go to solution Solved by fizzlesticks,

You're creating a new variable inside init called windowID instead of using your instance variable. So when you pass the windowID to swapbuffers, the variable hasn't been initialized.

As a project I am trying to make a really simple engine using lwjgl 3. What I have written so far should just display a blank window running on a separate thread when started. But instead a window opens for a fraction of a second then I get a nullpointer exception in thread 0. Although I don't know much about java I find this a bit odd as all of my lwjgl code is written to run on a separate thread (usually ends up being thread 8). If possible I want to keep the graphics side of things on a separate thread.

 

The error:

Exception in thread "Thread-0" java.lang.NullPointerException
	at org.lwjgl.system.Checks.checkPointer(Checks.java:100)
	at org.lwjgl.glfw.GLFW.glfwSwapBuffers(GLFW.java:2857)
	at tungsten.graphics.Window.run(Window.java:40)
	at java.lang.Thread.run(Thread.java:745)

The app contains three files:

Game.java:

package TestGame;

import engine.application.App;

public class Game extends App {
	
	public static void main(String[] args) {
		new Game().start("Test", 800, 600);
	}
	
}

App.java:

package engine.application;

import engine.graphics.Window;

public class App {
	
	public boolean running = false;
	
	private String title;
	private Window window;
	private Thread windowThread;
	
	public void start(String title, int width, int height) {
		running = true;
		window = new Window(this, title, width, height);
		windowThread = new Thread(window);
		windowThread.start();
	}
	
	public String getTitle() {
		return title;
	}
	
}

Window.java:

package engine.graphics;

import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.*;

import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL;

import engine.application.App;

public class Window implements Runnable {
	
	private App app;
	private String title;
	private int width;
	private int height;
	private long windowID;
	
	public Window(App app, String title, int width, int height) {
		this.app = app;
		this.title = title;
		this.width = width;
		this.height = height;
	}
	
	public void render() {
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	}
	
	@Override
	public void run() {
		init();
		
		glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		
		while (app.running) {
			render();
			
			glfwSwapBuffers(windowID);
			glfwPollEvents();
			
			if (glfwWindowShouldClose(windowID) == GL_TRUE) {
				app.running = false;
			}
		}
	}
	
	private void init() {
		if (glfwInit() != GL_TRUE) {
		    System.err.println("ERROR: COULD NOT INIT GLFW");
		    System.exit(1);
		}
		
		long windowID = glfwCreateWindow(width, height, title, NULL, NULL);
		if (windowID == NULL) {
		    System.err.println("ERROR: COULD NOT CREATE WINDOW");
		    System.exit(1);
		}
		
		GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
                glfwSetWindowPos(
                  windowID,
                  (vidmode.width() - width) / 2,
                  (vidmode.height() - height) / 2
                );
		
        	glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
		
		glfwMakeContextCurrent(windowID);
		glfwSwapInterval(1);
		glfwShowWindow(windowID);
		GL.createCapabilities();
	}
}

 

~ Luc Luc

Link to comment
https://linustechtips.com/topic/584923-java-lwjgl-3-exception/
Share on other sites

Link to post
Share on other sites

The first thing I'd do is look at

GLFW.java:2857

 

and

Checks.java:100

 

to see what could be null.

 

If I couldn't see it at a first glance, I'd put a break point there and step over/into until I got to that point, and see what the debugger tells me the values are.

			glfwSwapBuffers(windowID);

is line 40 according to notepad++

 

Is your windowID null, by any chance?

AMD Ryzen 7800 X3D, MSI B650 Project Zero, Antec C5, Gigabyte RTX 4080 Super Aero

 

Nikon D500 | Nikon 300mm f/4 PF  | Nikon 200-500 f/5.6 | Nikon 50mm f/1.8 | Tamron 70-210 f/4 VCII | Sigma 10-20 f/3.5 | Nikon 17-55 f/2.8 | Tamron 90mm F2.8 SP Di VC USD Macro | Neewer 750II

Link to comment
https://linustechtips.com/topic/584923-java-lwjgl-3-exception/#findComment-7627639
Share on other sites

Link to post
Share on other sites

1 minute ago, fizzlesticks said:

You're creating a new variable inside init called windowID instead of using your instance variable. So when you pass the windowID to swapbuffers, the variable hasn't been initialized.

Herp derp thank you kind sir.

~ Luc Luc

Link to comment
https://linustechtips.com/topic/584923-java-lwjgl-3-exception/#findComment-7627646
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×