NullPointerException

Started by
9 comments, last by Eckhart 12 years, 4 months ago
Well, I've been trying to learn some Java for a while, but I'm having some trouble with the following code:

package net.devox.basic;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;



/*
* Title: Game
* Version: 1.0.0
* Desc: This class is used to control and pack together the
* methods required to run the games logic in a central
* location.
*/



public class Game extends Canvas
{
//buffer for off screen drawing
private BufferedImage buffer;

private static final long serialVersionUID = 1L;

private void init()
{
//turn paint off
this.setIgnoreRepaint(true);
//set size
this.setSize(GUI.WIDTH, GUI.HEIGHT);
//set Focusable
this.setFocusable(true);
//create buffer
buffer = new BufferedImage(GUI.WIDTH, GUI.HEIGHT, BufferedImage.TYPE_INT_ARGB);


}

public void start()
{
//initialize game
this.init();
//start loop

while(true){
try{
this.update();
this.drawbuffer();
this.drawscreen();
}catch(Exception e){e.printStackTrace(); Main.close();}
}

}

private void update()
{
return;
}

private void drawbuffer()
{
//set buffer
Graphics buff = buffer.createGraphics();

//do drawing logic
drawrect(buff, 10, 10, 10, 10);

//clear buffer
buff.dispose();
}

private void drawscreen()
{
Graphics2D g2d = (Graphics2D)getGraphics();

// Render Buffer to Screen
g2d.drawImage(buffer, 0, 0, this);

Toolkit.getDefaultToolkit().sync();

// Dispose of Graphics2D
g2d.dispose();
}


//Generic render method. Change this.
private void drawrect(Graphics buff, int x, int y, int width, int height)
{
buff.setColor(Color.BLACK);
buff.drawRect(x, y, width, height);
}
}

( http://pastebin.com/A8hEgMMf )
I get the error:

java.lang.NullPointerException
at net.devox.basic.Game.drawscreen(Game.java:81)
at net.devox.basic.Game.start(Game.java:57)
at net.devox.basic.Main.main(Main.java:19)

when I run it.

Does anyone have any advice or know how to fix the error? I'd greatly appreciate any feedback.

Thank you for your time.
Advertisement
Time to get acquainted with a debugger.

Obviously the null pointer occurred at line 86. Which line is this exactly?
Then look at that line, which variable could be null there?

PasteBin says, line 86 is "[color="#000000"]g2d[color="#666600"].[color="#000000"]dispose[color="#666600"]();". Which does not make much sense.
If g2d was null, it would have thrown at drawImage already. Unless the null pointer was an inner one in Graphics2d, which occurred, because sync cleaned something up.

Other possibility: The stack trace is wrong (would be weird) and the null pointer occurs at Toolkit.getDefaultToolkit().sync() (ie. Toolkit.getDefaultToolkit() returns null, which i personally don't believe).

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

I think I messed up somewhere while posting this...
It highlights line 81,[color=#181818][font=monospace]

g2d.[/font][color=#006633][font=monospace]

drawImage[/font][color=#009900][font=monospace]

([/font][color=#181818][font=monospace]

buffer, [/font][color=#CC66CC][font=monospace]

0[/font][color=#181818][font=monospace]

, [/font][color=#CC66CC][font=monospace]

0[/font][color=#181818][font=monospace]

, [/font][color=#000000][font=monospace]

this[/font][color=#009900][font=monospace]

);[/font]

I have no idea what could be null there. I mean, I guess it has to be buffer, but I clearly defined buffer in the init() method.


I think I messed up somewhere while posting this...
It highlights line 81,[color=#181818][font=monospace]

g2d.[/font][color=#006633][font=monospace]

drawImage[/font][color=#009900][font=monospace]

([/font][color=#181818][font=monospace]

buffer, [/font][color=#CC66CC][font=monospace]

0[/font][color=#181818][font=monospace]

, [/font][color=#CC66CC][font=monospace]

0[/font][color=#181818][font=monospace]

, [/font][color=#000000][font=monospace]

this[/font][color=#009900][font=monospace]

);[/font]

I have no idea what could be null there. I mean, I guess it has to be buffer, but I clearly defined buffer in the init() method.


Where do you call the init() method?

I think I messed up somewhere while posting this...
It highlights line 81,[color=#181818][font=monospace]

g2d.[/font][color=#006633][font=monospace]

drawImage[/font][color=#009900][font=monospace]

([/font][color=#181818][font=monospace]

buffer, [/font][color=#CC66CC][font=monospace]

0[/font][color=#181818][font=monospace]

, [/font][color=#CC66CC][font=monospace]

0[/font][color=#181818][font=monospace]

, [/font][color=#000000][font=monospace]

this[/font][color=#009900][font=monospace]

);[/font]

I have no idea what could be null there. I mean, I guess it has to be buffer, but I clearly defined buffer in the init() method.


No, it can only be the graphics context.
Take a look at getGraphics. You won't get a context if you don't have a parent component for the canvas.

[quote name='Eckhart' timestamp='1326008728' post='4900571']
I think I messed up somewhere while posting this...
It highlights line 81,[color=#181818][font=monospace]

g2d.[/font][color=#006633][font=monospace]

drawImage[/font][color=#009900][font=monospace]

([/font][color=#181818][font=monospace]

buffer, [/font][color=#CC66CC][font=monospace]

0[/font][color=#181818][font=monospace]

, [/font][color=#CC66CC][font=monospace]

0[/font][color=#181818][font=monospace]

, [/font][color=#000000][font=monospace]

this[/font][color=#009900][font=monospace]

);[/font]

I have no idea what could be null there. I mean, I guess it has to be buffer, but I clearly defined buffer in the init() method.


Where do you call the init() method?
[/quote]

in the beginning of the start() method.



No, it can only be the graphics context.
Take a look at getGraphics. You won't get a context if you don't have a parent component for the canvas.

I'm not sure what you're asking. The class extends canvas, if that's what you mean.
The null pointer is g2d, I believe: in line 78, if (Graphics2D)getGraphics() doesn't return a valid pointer or fully instantiated object reference, then g2d is a bad pointer, and trying to dereference it with the dot operator (in line 81) generates your null pointer exception. You can check this by testing g2d directly after the instantiation call if you want. Look at the getGraphics() method and see what it relies on to work.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

Fixed the error :D turns out it was in my main function :/ I still don't understand it....
I was calling the Game.start() method in the main method.
I decided to change it to see what would happen.
I put the Game.start() method in the constructor of the GUI class.
GUI() is called in the main method.
Why does it work now, but didn't work earlier?
Without seeing the source code in its entirety, I'd wager it has to do with the order of object initialization. Your code can be syntactically sound, but if the overall design makes assumptions on objects being initialized correctly (to include method calls that just assume their "this" pointer is already valid) you won't see the problem until runtime.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

Because if you call it in your main method the Graphics object from the GUI might not be initialized yet. You've created a race situation because your main method is in one thread and the GUI (i assume it's a JFrame or something) runs in the GUI thread.

You also shouldn't make that start method the way you did. You should have an object that Implements Runnable and use that to Initilized a Thread and you can start that. Also never do a while(true). Try creating a static volatile boolean variable called isRunning and make it equal true. That way when you need to stop the thread gracefully you just make isRunning = false so that on the next iteration of the loop it stops.

This topic is closed to new replies.

Advertisement