Results 1 to 20 of 20
  1. #1
    JavaWizKid is offline Member
    Join Date
    Jul 2011
    Posts
    19
    Rep Power
    0

    Question Copied code from this tutorial- BufferStrategy

    Java Tutorial - Making a 2D Game Part 6 - YouTube

    However, the code compiled perfectly but when it runs it errors

    Exception in thread "Thread-3" java.lang.NullPointerException
    at GameCanvas.Render(GameCanvas.java:72)
    at GameCanvas.run(GameCanvas.java:50)
    at java.lang.Thread.run(Thread.java:680)

    Java Code:
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.image.BufferStrategy;
    
    @SuppressWarnings("serial")
    public class GameCanvas extends Canvas implements Runnable
    {
    	int FWidth = 800;
    	int FHeight = 600;
    	
    	public long period = 10;
    	
    	public BufferStrategy Buffer;
    	public Graphics graphics;
    	
    	public AppleEntity apple;
    	
    	private Thread t;
    	
    	public GameCanvas()
    	{
    		this.setIgnoreRepaint(true);
    		this.setBounds(0,0,FWidth,FHeight);
    		this.setBackground(Color.white);
    		
    		this.setVisible(true);
    		
    		apple = new AppleEntity("Apple.png",200,200);
    	}
    	
    	public void addNotify()
    	{
    		if(t == null)
    		{
    			t = new Thread(this);
    			t.start();
    		}
    	}
    	
    	@SuppressWarnings("static-access")
    	public void run()
    	{
    		while(true)
    		{
    			long beginTime = System.currentTimeMillis();
    			
    			Update();
    			Render();
    			Draw();
    			
    			long timeTaken = System.currentTimeMillis() - beginTime;
    			long sleepTime = period - timeTaken;
    			
    			try
    			{
    				t.sleep(sleepTime);
    			}
    			catch(Exception e) { }
    			
    		}
    	}
    	
    	public void Update()
    	{
    		apple.Fall();
    	}
    	
    	public void Render()
    	{
    		graphics = Buffer.getDrawGraphics();
    		graphics.setColor(Color.white);
    		graphics.fillRect(0,0,FWidth,FHeight);
    		
    		//Paint stuff
    		apple.Draw(graphics);
    	}
    	
    	public void Draw()
    	{
    		if(!Buffer.contentsLost())
    		{
    			Buffer.show();
    			
    			if(graphics != null)
    			{
    				graphics.dispose();
    			}
    		}
    	}
    }
    I have never done BufferStrategy before, what is wrong?

    Thanks

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Copied code from this tutorial- BufferStrategy

    Exception in thread "Thread-3" java.lang.NullPointerException
    at GameCanvas.Render(GameCanvas.java:72)
    Look at the statement at line 72 and see which variable has a null value. If you can not tell which one, add a println just before line 72 that prints out the values of all the variable used on line 72.
    When you get the variable that has a null value, back track in your code to see why it does not have a valid value.

  3. #3
    JavaWizKid is offline Member
    Join Date
    Jul 2011
    Posts
    19
    Rep Power
    0

    Default Re: Copied code from this tutorial- BufferStrategy

    Java Code:
    public void Render()
    	{
    		System.out.println("1 "+graphics);
    		System.out.println("2 "+Buffer);
    		graphics = Buffer.getDrawGraphics();
    		graphics.setColor(Color.white);
    		graphics.fillRect(0,0,FWidth,FHeight);
    		
    		//Paint stuff
    		apple.Draw(graphics);
    	}
    1 null
    Exception in thread "Thread-3" 2 null
    java.lang.NullPointerException
    at GameCanvas.Render(GameCanvas.java:73)
    at GameCanvas.run(GameCanvas.java:49)
    at java.lang.Thread.run(Thread.java:680)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Copied code from this tutorial- BufferStrategy

    Where is the variable Buffer given a valid value? Check back through your code and make sure graphics is getting a valid value.

    Why print out the value of graphics before the assignment statement that gives it a value???
    Last edited by Norm; 11-14-2011 at 06:13 PM.

  5. #5
    JavaWizKid is offline Member
    Join Date
    Jul 2011
    Posts
    19
    Rep Power
    0

    Default Re: Copied code from this tutorial- BufferStrategy

    It doesn't have one, what value do I need to give it?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Copied code from this tutorial- BufferStrategy

    what value do I need to give it?
    That depends.
    What is the method Render() supposed to do? Why do you call it?
    What objects does it need to do it job?

    Normally drawing methods are called from a paintComponent method and are passed the Graphics object that they receive as argument.

  7. #7
    JavaWizKid is offline Member
    Join Date
    Jul 2011
    Posts
    19
    Rep Power
    0

    Default Re: Copied code from this tutorial- BufferStrategy

    A lot of unneeded code but I posted it all to find this problem.

    Java Code:
    import java.awt.Graphics;
    import java.awt.Rectangle;
    
    public abstract class Entity
    {
    	protected double x;
    	protected double y;
    	
    	protected Sprite sprite;
    	
    	protected double Dy;
    	protected double Dx;
    	
    	private Rectangle me = new Rectangle();
    	private Rectangle him = new Rectangle();
    	
    	public Entity(String ref,int x, int y)
    	{
    		this.sprite = ImageLoader.get().getSprite(ref);
    		this.x = x;
    		this.y = y;
    	}
    	
    	public void move(long delta)
    	{
    		x += (delta * Dx)/1000;
    		y += (delta * Dy)/1000;
    	}
    	
    	public void setHorizontalMovement(double dx)
    	{
    		this.Dx = dx;
    	}
    	
    	public void setVerticalMovement(double dy)
    	{
    		this.Dy = dy;
    	}
    	
    	public double getHorizontalMovement()
    	{
    		return Dx;
    	}
    	
    	public double getVerticalMovement()
    	{
    		return Dy;
    	}
    	
    	public void Draw(Graphics g)
    	{
    		sprite.Draw(g,(int) x,(int) y);
    	}
    	
    	public int getX()
    	{
    		return (int) x;
    	}
    	
    	public int getY()
    	{
    		return (int) y;
    	}
    	
    	public boolean collidesWidth(Entity e)
    	{
    		me.setBounds((int) x, (int) y,sprite.getWidth(),sprite.getHeight());
    		him.setBounds((int) e.x,(int) e.y, e.sprite.getWidth(),e.sprite.getHeight());
    		
    		return me.intersects(him);
    	}
    	
    	//public abstract void collidesWidth(Entity e);
    }
    Java Code:
    import java.awt.Graphics;
    import java.awt.Image;
    
    public class Sprite
    {
    	private Image image;
    	
    	public Sprite(Image image)
    	{
    		this.image = image;
    	}
    	
    	public int getWidth()
    	{
    		return image.getWidth(null);
    	}
    	
    		public int getHeight()
    	{
    		return image.getHeight(null);
    	}
    	
    	public void Draw(Graphics g, int x, int y)
    	{
    		g.drawImage(image,x,y,null);
    	}
    }
    Java Code:
    import java.awt.GraphicsConfiguration;
    import java.awt.GraphicsEnvironment;
    import java.awt.Image;
    import java.awt.Transparency;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.HashMap;
    
    import javax.imageio.ImageIO;
    
    public class ImageLoader
    {
    	private static ImageLoader single = new ImageLoader();
    	
    	public static ImageLoader get()
    	{
    		return single;
    	}
    	
    	private HashMap<String,Sprite> Images = new HashMap<String,Sprite>();
    	
    	public Sprite getSprite(String ref)
    	{
    		if(Images.get(ref) != null)
    		{
    			return (Sprite) Images.get(ref);
    		}
    		
    		BufferedImage sourceImage = null;
    		
    		try
    		{
    			sourceImage = ImageIO.read(new File (ref));
    		}
    		catch(IOException e)
    		{
    			System.out.println("Unable to load: " + ref);
    		}
    		
    		GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
    		Image image = gc.createCompatibleImage(sourceImage.getWidth(),sourceImage.getHeight(),Transparency.BITMASK);
    		
    		image.getGraphics().drawImage(sourceImage,0,0,null);
    		
    		Sprite sprite = new Sprite(image);
    		Images.put(ref,sprite);
    		
    		return sprite;
    	}
    }
    Last edited by JavaWizKid; 11-14-2011 at 06:27 PM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Copied code from this tutorial- BufferStrategy

    What is the problem now? What is all the code for?

  9. #9
    JavaWizKid is offline Member
    Join Date
    Jul 2011
    Posts
    19
    Rep Power
    0

    Default Re: Copied code from this tutorial- BufferStrategy

    It's all part of render I guess :) The apple extends entity which extends sprite which uses ImageLoader to load the image. However, the error is being called before apple.Draw(graphics) gets called.

    This line graphics = Buffer.getDrawGraphics(); specifically.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Copied code from this tutorial- BufferStrategy

    If the variable: Buffer is null when you try to use it, then Where is the variable Buffer given a value?

  11. #11
    JavaWizKid is offline Member
    Join Date
    Jul 2011
    Posts
    19
    Rep Power
    0

    Default Re: Copied code from this tutorial- BufferStrategy

    It isn't. It is only declared
    Java Code:
    public BufferStrategy Buffer;
    What would give it? CreateBufferStrategy?

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Copied code from this tutorial- BufferStrategy

    If its only declared then it will be null.

    Where did you get this code? Does it have examples on how to make it work?
    Have you looked in the API doc for the BufferStrategy class to see if there are any clues there?

  13. #13
    JavaWizKid is offline Member
    Join Date
    Jul 2011
    Posts
    19
    Rep Power
    0

    Default Re: Copied code from this tutorial- BufferStrategy

    YouTube link at the top. The source isn't provided which is quite annoying. graphics will be null because it is based off Buffer. What value do I give Buffer?

    The code works fine if i comment out render and draw, so there are no errors elsewhere. Doesn't draw anything though.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Copied code from this tutorial- BufferStrategy

    Have you looked in the API doc for the BufferStrategy class to see if there are any clues there?

  15. #15
    JavaWizKid is offline Member
    Join Date
    Jul 2011
    Posts
    19
    Rep Power
    0

    Default Re: Copied code from this tutorial- BufferStrategy

    The docs say this.
    Java Code:
     // Check the capabilities of the GraphicsConfiguration
     ...
    
     // Create our component
     Window w = new Window(gc);
    
     // Show our window
     w.setVisible(true);
    
     // Create a general double-buffering strategy
     w.createBufferStrategy(2);
     BufferStrategy strategy = w.getBufferStrategy();
    
     // Render loop
     while (!done) {
        Graphics g = strategy.getDrawGraphics();
        // Draw to graphics
        ...
        strategy.show();
     }
    
     // Dispose the window
     w.setVisible(false);
     w.dispose();

    So I added this to my GameCanvas() function.

    Java Code:
    	public GameCanvas()
    	{
    		this.setIgnoreRepaint(true);
    		this.setBounds(0,0,FWidth,FHeight);
    		this.setBackground(Color.white);
    		
    		this.createBufferStrategy(2);
    		Buffer = getBufferStrategy();
    		
    		this.setVisible(true);
    		
    		apple = new AppleEntity("Apple.png",200,200);
    	}

    New error, yay!

    Exception in thread "main" java.lang.IllegalStateException: Component must have a valid peer
    at java.awt.Component$FlipBufferStrategy.createBuffer s(Component.java:3843)
    at java.awt.Component$FlipBufferStrategy.<init>(Compo nent.java:3817)
    at java.awt.Component$FlipSubRegionBufferStrategy.<in it>(Component.java:4358)
    at java.awt.Component.createBufferStrategy(Component. java:3699)
    at java.awt.Canvas.createBufferStrategy(Canvas.java:1 66)
    at java.awt.Component.createBufferStrategy(Component. java:3623)
    at java.awt.Canvas.createBufferStrategy(Canvas.java:1 41)
    at GameCanvas.<init>(GameCanvas.java:27)
    at GameFrame.main(GameFrame.java:16)

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Copied code from this tutorial- BufferStrategy

    java.lang.IllegalStateException: Component must have a valid peer
    Does the component you are working with have a valid peer? The JVM says not.
    You need to research when a GUI component gets a peer. I've never used this class so I have no links to pass on.
    Is the component a GUI item and is it visible?

  17. #17
    JavaWizKid is offline Member
    Join Date
    Jul 2011
    Posts
    19
    Rep Power
    0

    Default Re: Copied code from this tutorial- BufferStrategy

    Java Code:
    import javax.swing.JFrame;
    
    public class GameFrame
    {
    	public static void main(String args[])
    	{
    		int FWidth = 800;
    		int FHeight = 600;
    		
    		JFrame frame = new JFrame("Game Name");
    		
    		frame.setIgnoreRepaint(true);
    		frame.setBounds(0,0,FWidth,FHeight);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		GameCanvas game = new GameCanvas();
    		
    		frame.add(game);
    		
    		frame.setVisible(true);
    		frame.createBufferStrategy(2);
    		
    		game.Buffer = frame.getBufferStrategy();
    	}
    }
    I've put them here now instead. both logs output null again with

    Exception in thread "Thread-3" java.lang.NullPointerException
    at GameCanvas.Render(GameCanvas.java:76)
    at GameCanvas.run(GameCanvas.java:52)
    at java.lang.Thread.run(Thread.java:680)

    No idea what is wrong and they shouldn't be null.

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Copied code from this tutorial- BufferStrategy

    What variable at line 76 is null? When/where do you give that variable a value?

    Do you give it a value BEFORE your code tries to use it?

  19. #19
    JavaWizKid is offline Member
    Join Date
    Jul 2011
    Posts
    19
    Rep Power
    0

    Default Re: Copied code from this tutorial- BufferStrategy

    Cracked it! Moved frame.add(game); below the buffer declaration. Fantastic! Thanks

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Copied code from this tutorial- BufferStrategy

    Glad you figured it out.

Similar Threads

  1. Replies: 8
    Last Post: 06-15-2011, 05:26 AM
  2. BufferStrategy in JFrame
    By dbomb in forum AWT / Swing
    Replies: 3
    Last Post: 05-23-2011, 01:43 AM
  3. BufferStrategy
    By Shadowdude04 in forum AWT / Swing
    Replies: 0
    Last Post: 03-13-2010, 02:59 PM
  4. BufferStrategy and white rows of pixels
    By TobyLobster in forum Java 2D
    Replies: 2
    Last Post: 12-29-2008, 01:49 PM
  5. how can objects themselves be copied???
    By ishakteyran in forum New To Java
    Replies: 1
    Last Post: 12-29-2007, 10:04 AM

Tags for this Thread

Posting Permissions

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