Graphics Null Pointer Exception

java.lang.NullPointerException when trying to retrieve Canvas Graphics

Page 1 of 1

3 Replies - 5178 Views - Last Post: 30 January 2010 - 09:16 PM Rate Topic: -----

#1 painkiller102  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 16
  • View blog
  • Posts: 281
  • Joined: 27-February 08

Graphics Null Pointer Exception

Posted 30 January 2010 - 08:00 PM

I have used Canvas several times now since PBL suggested them to me, however now I am having a problem with a canvas and cannot seem to figure it out. The flow is right and i do not understand why i am getting this null pointer exception. My code is:

public class Game extends JFrame
{
	public Game()
	{
		this.setUndecorated(true);

		initComponents();

		this.setSize(500,500);

		this.setVisible(true);


		GameCanvas c = new GameCanvas();
		this.add(c,BorderLayout.CENTER);
	}

	public class GameCanvas extends Canvas
	{
		boolean repaintInProgress = false;

		public GameCanvas()
		{
			setFocusable(true);
			
			this.setBackground(Color.BLACK);
			this.setSize(500.500);
			
			setIgnoreRepaint(true);
			UpdateTimer update = new UpdateTimer(this);
			new Timer(16, update).start();
		}

		public void myRepaint()
		{
			if(repaintInProgress)
				return;
			repaintInProgress = true;
			Dimension size = getSize();
			
			BufferStrategy strategy = getBufferStrategy();
			Graphics graphics = strategy.getDrawGraphics();
			graphics.fillRect(0, 0, size.width, size.height);

			//draw stuff

			repaintInProgress = false;
		}

		public class UpdateTimer implements ActionListener
		{
			GameCanvas gc;

			UpdateTimer(GameCanvas gc)
			{
				this.gc = gc;
			}
			public void actionPerformed(ActionEvent e)
			{
				gc.myRepaint();
			}
		}
	}
}



The code will get to the line:

Graphics graphics = strategy.getDrawGraphics();



before erroring out. Now from what i understand about java graphics, the only reason this should happen is because the canvas is not shown before the line Graphics graphics = strategy.getDrawGraphics(); is called. BUT i have the JFrame show before the canvas is even created and added to the JFrame?! I'm a bit confused as well beacause i have made a similar game skeleton and it works fully, but this one will not?! Any Help is greatly appreciated and thanks in advance for any help! Lookin forward to an explination on this one! :)

Is This A Good Question/Topic? 0
  • +

Replies To: Graphics Null Pointer Exception

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: Graphics Null Pointer Exception

Posted 30 January 2010 - 08:22 PM

Actually, in this scenario, I would use a JPanel because much of it is already set up. Let me demonstrate.
public class GameCanvas extends JPanel
	{
		public GameCanvas()
		{
			setFocusable(true);

			this.setBackground(Color.BLACK);
			this.setSize(500,500);

			UpdateTimer update = new UpdateTimer(this);
			new Timer(16, update).start(); // GOOD!
		}
 
		// Overwrite paintComponent. Do all of your painting in here...
		// This is called on repaint()
		public void paintComponent(Graphics g)
		{
			Dimension size = getSize();
			g.fillRect(0, 0, size.width, size.height);
		}

		public class UpdateTimer implements ActionListener
		{
			GameCanvas gc;

			UpdateTimer(GameCanvas gc)
			{
				this.gc = gc;
			}
			public void actionPerformed(ActionEvent e)
			{
				// Just call repaint.
				// It will call paintComponent.
				gc.repaint();
			}
		}
	}



If you have any questions, let me know!
Was This Post Helpful? 1
  • +
  • -

#3 painkiller102  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 16
  • View blog
  • Posts: 281
  • Joined: 27-February 08

Re: Graphics Null Pointer Exception

Posted 30 January 2010 - 08:53 PM

Thanks for the help. I have not tried it yet, but that does seem a bit better than the way I'm going. I still am unsure of the canvas not working tho. But what can we say. things break at odd times....

-:EDIT:-
I have tried the code and it is in fact working as it should be, thanks for the help Dogstopper

This post has been edited by painkiller102: 30 January 2010 - 09:24 PM

Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: Graphics Null Pointer Exception

Posted 30 January 2010 - 09:16 PM

Hey...look at my blog...I answer many of these questions in my Game Tutorials section.

http://www.dreaminco...opper/index.php?

EDIT: No problem. Glad to have helped!

This post has been edited by Dogstopper: 30 January 2010 - 09:41 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1