Null Pointer Exception when using a superclass?

Started by johnpooley3, June 21, 2012, 18:09:14

Previous topic - Next topic

johnpooley3

I am writing some very basic code, which will take mouse clicks, and put vertexes at them. When I use a superclass that I wrote called BasicLWJGLapp then my process method throws a NullPointerException. When I copypasta the classes into a single class, the error is not thrown. What is causing a NullPointerException of MyLWJGLapp.java:38? Thanks in advance!

Superclass:

package BasicLWJGL;

import static org.lwjgl.opengl.GL11.*;

import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

public class BasicLWJGLapp {
	
	int notfps = 0;
	long lastFrame = 0;
	long lastFPS = 0;
	
	public int fps = 0;
	
	public BasicLWJGLapp(int width, int height, boolean startfull, boolean fullResolution, String displayTitle, boolean fpsInTitle)
	{
		try{
			setDisplayMode(width, height, fullResolution, startfull);
			Display.create();
		}catch (LWJGLException e){
			e.printStackTrace();
			System.exit(0);
		}
		
		lastFPS = getTime();
		
		setup(width, height);
		
		Display.setTitle(displayTitle);
		
		while(!Display.isCloseRequested()){
			draw();
			Display.update();
			updateFPS();
			process();
			
			if(!startfull)
				Display.sync(Display.getDesktopDisplayMode().getFrequency());
			
			if(fpsInTitle)
				Display.setTitle(displayTitle+" FPS: "+fps);
		}
		Display.destroy();
	}
	
	public BasicLWJGLapp(int width, int height, String displayTitle)
	{
		this(width, height, false, true, displayTitle, false);
	}
	
	public BasicLWJGLapp(String displayTitle)
	{
		this(854, 480, displayTitle);
	}
	
	public BasicLWJGLapp()
	{
		this("LWJGL");
	}
	
	public final void setDisplayMode(int width, int height, boolean fullResolution, boolean fullScreen)
	{
		try{
			if(fullScreen){
				if(fullResolution)
					Display.setDisplayMode(Display.getDesktopDisplayMode());
				else
					Display.setDisplayMode(new DisplayMode(width, height));
				Display.setFullscreen(fullScreen);
				Display.setVSyncEnabled(fullScreen);
			}else{
				Display.setDisplayMode(new DisplayMode(width, height));
			}
		}catch (LWJGLException e){
			e.printStackTrace();
			System.exit(0);
		}
	}
	
	private void updateFPS()
	{
		if(getTime()-lastFPS>1000){
			fps = notfps;
			notfps = 0;
			lastFPS += 1000;
		}
		notfps++;
	}
	
	private long getDelta()
	{
		long delta = getTime()-lastFrame;
		lastFrame = getTime();
		return delta;
	}
	
	public final long getTime()
	{
		return (Sys.getTime()*1000)/Sys.getTimerResolution();
	}
	
	private void setup(int width, int height)
	{
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		glOrtho(0, width, 0, height, 1, -1);
		glMatrixMode(GL_MODELVIEW);
	}
	
	public void process()
	{
		
	}
	
	public void draw()
	{
		glBegin(GL_QUADS);
		int ten = 10;
		int twen = 20;
		glVertex2i(ten, ten);
		glVertex2i(twen, ten);
		glVertex2i(twen, twen);
		glVertex2i(ten, twen);
		glEnd();
	}
}


Subclass (that is run by my runner)
package my;

import java.util.ArrayList;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;

import BasicLWJGL.BasicLWJGLapp;
import static org.lwjgl.opengl.GL11.*;

public class MyLWJGLapp extends BasicLWJGLapp{

	ArrayList<int[]> coords = new ArrayList<int[]>();
	
	public MyLWJGLapp()
	{
		super("My App");
	}
	
	public void draw(){
		glBegin(GL_POINTS);
		
		try{
			for(int[] a : coords){
				glVertex2i(a[0], a[1]);
			}
		}catch(NullPointerException f*ckyou){}
		
		glEnd();
		process();
	}
	
	public void process()
	{
		while(Mouse.next()){
			if(Mouse.getEventButton()==0){
				coords.add(new int[] {Mouse.getX(), Mouse.getY()});
			}else if(Mouse.getEventButton()==1) {
				coords.clear();
			}
		}
	}
}

CodeBunny

Do you still get a NullPointerException when you remove your overridden draw() method?

johnpooley3

Quote from: CodeBunny on June 21, 2012, 18:53:19
Do you still get a NullPointerException when you remove your overridden draw() method?

Yes I still get a NPE on line 38 of MyLWJGLapp when lines 21-32 are commented out, removing the override for draw

Fool Running

Just a guess, but MyLWJGLapp is probably not being initialized (i.e. coords hasn't been set to a new ArrayList) because you are calling the process method inside the constructor of the base class (in your main loop). I would suggest not putting your main loop inside a constructor anyways.
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

brianmanee

The NullReferenceException is designed as a valid runtime condition that can be thrown and caught in normal program flow. It indicates that you are trying to access member fields, or function types, on an object reference that points to null. That means the reference to an Object which is not initialized.

Manee