Results 1 to 4 of 4
  1. #1
    Aldane is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Unhappy Null Pointer Exeption

    I'm new to all these things and I am trying to do 'double buffering' in my game so that the pictures would not flicker.

    Then when I tried to run the game, eclipse said that it had a NullPointerExeption. When I debugged it, it said that it had an error on this line "bufferGraphics = offscreen.getGraphics();".

    Please Help :)

    Java Code:
    public class Board extends Canvas
    {
    	Image[] backgrounds;
    	Image[] bloatMeter;
    	Character pig;
    	
    	Graphics bufferGraphics;
    	Image offscreen;
    	
    	int backgroundNo;
    	int meter;
    	
    	//Perks Pictures (Sample Only)
    	Image[] perks;
    	
    	public Board()
    	{
    		offscreen = createImage(600, 700);
    		[B]bufferGraphics = offscreen.getGraphics(); [/B]
    		
    		//Background Pictures
    		backgroundNo = 0;
    		backgrounds = new Image[5];
    		
    		backgrounds[0] = new ImageIcon("Farm Background.png").getImage();
    		backgrounds[1] = new ImageIcon("Background Blue.png").getImage();
    		
    		//Bloat Meter Pictures
    		meter = 0;
    		bloatMeter = new Image[4];
    		
    		bloatMeter[0] = new ImageIcon("Bloat Meter 1.png").getImage();
    		bloatMeter[1] = new ImageIcon("Bloat Meter 2.png").getImage();
    		bloatMeter[2] = new ImageIcon("Bloat Meter 3.png").getImage();
    		bloatMeter[3] = new ImageIcon("Bloat Meter 4.png").getImage();
    		
    		//Perk Pictures (Sample Only) -Create new class
    		perks = new Image[3];
    		
    		perks[0] = new ImageIcon("Cake.png").getImage();
    		perks[1] = new ImageIcon("Watermelon.png").getImage();
    		perks[2] = new ImageIcon("Donut.png").getImage();
    		
    		//New Pig Object
    		pig = new Character(); 
    		
    		addMouseListener(new MouseEvents());
    		addMouseMotionListener(new MouseEvents());
    		addKeyListener(new KeyboardEvents());
    	}
    	
    	public void paint(Graphics g)
    	{
    		//Background
    		bufferGraphics.drawImage(backgrounds[backgroundNo], 0, 0, this);
    		
    		//Height - Sample Only
    		bufferGraphics.drawString("Height: 00000", 5, 20);
    	
    		//Bloat Meter
    		bufferGraphics.drawImage(bloatMeter[meter], 480, 5, this);
    		
    		//Perks - Sample Only
    		bufferGraphics.drawImage(perks[0], 200, 70, this);
    		bufferGraphics.drawImage(perks[0], 350, 70, this);
    		
    		bufferGraphics.drawImage(perks[1], 150, 300, this);
    		bufferGraphics.drawImage(perks[1], 270, 300, this);
    		bufferGraphics.drawImage(perks[1], 400, 300, this);
    		
    		//Character
    		bufferGraphics.drawImage(pig.pigPictures[meter], pig.x - 40, pig.y, this);
    		
    		g.drawImage(offscreen, 0, 0, this);
    		
    		/** Old "Non buffered" stuff
    
    		//Background
    		g.drawImage(backgrounds[backgroundNo], 0, 0, this);
    		
    		//Height - Sample Only
    		g.drawString("Height: 00000", 5, 20);
    	
    		//Bloat Meter
    		g.drawImage(bloatMeter[meter], 480, 5, this);
    		
    		//Perks - Sample Only
    		g.drawImage(perks[0], 200, 70, this);
    		g.drawImage(perks[0], 350, 70, this);
    		
    		g.drawImage(perks[1], 150, 300, this);
    		g.drawImage(perks[1], 270, 300, this);
    		g.drawImage(perks[1], 400, 300, this);
    		
    		//Character
    		g.drawImage(pig.pigPictures[meter], pig.x - 40, pig.y, this);
    
    		**/
    	}
    	
    	public void update(Graphics g)
        {
             paint(g);
        }
    
        ....

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    Why are you using java.awt.Canvas and not javax.swing.JComponent/JPanel? Swing components are double buffered by default.

    As for your present problem, read the API for createImage(int, int) inherited from java.awt.Component.

    db

  3. #3
    Aldane is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    Wow! That was a quick fix :) I used canvas because our teacher used it as an example so I just copied him :P Thank You Very Much! :)

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 07:48 PM
  2. Null Pointer
    By theen3my in forum AWT / Swing
    Replies: 3
    Last Post: 10-03-2009, 03:10 PM
  3. Null pointer exception
    By talha06 in forum JDBC
    Replies: 5
    Last Post: 07-14-2009, 02:12 AM
  4. null pointer help
    By mayhewj7 in forum New To Java
    Replies: 5
    Last Post: 02-18-2009, 12:51 AM
  5. null pointer exeption
    By mj23brm in forum New To Java
    Replies: 4
    Last Post: 02-11-2009, 12:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •