Sign in to follow this  

NullPointerException

This topic is 2196 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well, I've been trying to learn some Java for a while, but I'm having some trouble with the following code:
[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);
}
}
[/CODE]
( [url="http://pastebin.com/A8hEgMMf"]http://pastebin.com/A8hEgMMf[/url] )
I get the error:
[CODE]
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)
[/CODE]
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.

Share this post


Link to post
Share on other sites
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][color="#666600"].[/color][color="#000000"]dispose[/color][color="#666600"]();[/color]". 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).

Share this post


Link to post
Share on other sites
I think I messed up somewhere while posting this...
It highlights line 81,[color=#181818][font=monospace][size=3] g2d.[/size][/font][/color][color=#006633][font=monospace][size=3]drawImage[/size][/font][/color][color=#009900][font=monospace][size=3]([/size][/font][/color][color=#181818][font=monospace][size=3]buffer, [/size][/font][/color][color=#CC66CC][font=monospace][size=3]0[/size][/font][/color][color=#181818][font=monospace][size=3], [/size][/font][/color][color=#CC66CC][font=monospace][size=3]0[/size][/font][/color][color=#181818][font=monospace][size=3], [/size][/font][/color][color=#000000][font=monospace][size=3][b]this[/b][/size][/font][/color][color=#009900][font=monospace][size=3]);[/size][/font][/color]

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.

Share this post


Link to post
Share on other sites
[quote name='Eckhart' timestamp='1326008728' post='4900571']
I think I messed up somewhere while posting this...
It highlights line 81,[color=#181818][font=monospace][size=3] g2d.[/size][/font][/color][color=#006633][font=monospace][size=3]drawImage[/size][/font][/color][color=#009900][font=monospace][size=3]([/size][/font][/color][color=#181818][font=monospace][size=3]buffer, [/size][/font][/color][color=#CC66CC][font=monospace][size=3]0[/size][/font][/color][color=#181818][font=monospace][size=3], [/size][/font][/color][color=#CC66CC][font=monospace][size=3]0[/size][/font][/color][color=#181818][font=monospace][size=3], [/size][/font][/color][color=#000000][font=monospace][size=3][b]this[/b][/size][/font][/color][color=#009900][font=monospace][size=3]);[/size][/font][/color]

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.
[/quote]
Where do you call the init() method?

Share this post


Link to post
Share on other sites
[quote name='Eckhart' timestamp='1326008728' post='4900571']
I think I messed up somewhere while posting this...
It highlights line 81,[color=#181818][font=monospace][size=3] g2d.[/size][/font][/color][color=#006633][font=monospace][size=3]drawImage[/size][/font][/color][color=#009900][font=monospace][size=3]([/size][/font][/color][color=#181818][font=monospace][size=3]buffer, [/size][/font][/color][color=#CC66CC][font=monospace][size=3]0[/size][/font][/color][color=#181818][font=monospace][size=3], [/size][/font][/color][color=#CC66CC][font=monospace][size=3]0[/size][/font][/color][color=#181818][font=monospace][size=3], [/size][/font][/color][color=#000000][font=monospace][size=3][b]this[/b][/size][/font][/color][color=#009900][font=monospace][size=3]);[/size][/font][/color]

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.
[/quote]
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.

Share this post


Link to post
Share on other sites
[quote name='RulerOfNothing' timestamp='1326009606' post='4900574']
[quote name='Eckhart' timestamp='1326008728' post='4900571']
I think I messed up somewhere while posting this...
It highlights line 81,[color=#181818][font=monospace][size=3] g2d.[/size][/font][/color][color=#006633][font=monospace][size=3]drawImage[/size][/font][/color][color=#009900][font=monospace][size=3]([/size][/font][/color][color=#181818][font=monospace][size=3]buffer, [/size][/font][/color][color=#CC66CC][font=monospace][size=3]0[/size][/font][/color][color=#181818][font=monospace][size=3], [/size][/font][/color][color=#CC66CC][font=monospace][size=3]0[/size][/font][/color][color=#181818][font=monospace][size=3], [/size][/font][/color][color=#000000][font=monospace][size=3][b]this[/b][/size][/font][/color][color=#009900][font=monospace][size=3]);[/size][/font][/color]

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.
[/quote]
Where do you call the init() method?
[/quote]

in the beginning of the start() method.


[quote name='6510' timestamp='1326023440' post='4900610']
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]
I'm not sure what you're asking. The class extends canvas, if that's what you mean.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote name='medv4380' timestamp='1326408996' post='4902138']
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.
[/quote]
Thanks, I think that's why.

Share this post


Link to post
Share on other sites
Sign in to follow this