Jump to content

  • Log In with Google      Sign In   
  • Create Account

NullPointerException


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 Eckhart   Members   -  Reputation: 108

Like
0Likes
Like

Posted 08 January 2012 - 01:15 AM

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.

Sponsor:

#2 Endurion   Crossbones+   -  Reputation: 3573

Like
0Likes
Like

Posted 08 January 2012 - 01:25 AM

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 "g2d.dispose();". 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>

#3 Eckhart   Members   -  Reputation: 108

Like
0Likes
Like

Posted 08 January 2012 - 01:45 AM

I think I messed up somewhere while posting this...
It highlights line 81, g2d.drawImage(buffer, 0, 0, this);

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.

#4 RulerOfNothing   Members   -  Reputation: 1160

Like
0Likes
Like

Posted 08 January 2012 - 02:00 AM

I think I messed up somewhere while posting this...
It highlights line 81, g2d.drawImage(buffer, 0, 0, this);

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?

#5 6510   Members   -  Reputation: 151

Like
0Likes
Like

Posted 08 January 2012 - 05:50 AM

I think I messed up somewhere while posting this...
It highlights line 81, g2d.drawImage(buffer, 0, 0, this);

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.

#6 Eckhart   Members   -  Reputation: 108

Like
0Likes
Like

Posted 09 January 2012 - 06:10 PM


I think I messed up somewhere while posting this...
It highlights line 81, g2d.drawImage(buffer, 0, 0, this);

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?


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.

#7 BCullis   Crossbones+   -  Reputation: 1813

Like
0Likes
Like

Posted 09 January 2012 - 11:26 PM

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
DeviantArt :: Because right-brain needs love too

#8 Eckhart   Members   -  Reputation: 108

Like
0Likes
Like

Posted 10 January 2012 - 11:48 PM

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?

#9 BCullis   Crossbones+   -  Reputation: 1813

Like
0Likes
Like

Posted 10 January 2012 - 11:59 PM

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
DeviantArt :: Because right-brain needs love too

#10 medv4380   Members   -  Reputation: 98

Like
0Likes
Like

Posted 12 January 2012 - 04:56 PM

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.

#11 Eckhart   Members   -  Reputation: 108

Like
0Likes
Like

Posted 12 January 2012 - 08:43 PM

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.

Thanks, I think that's why.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS