Results 1 to 5 of 5
  1. #1
    Elijah is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default NPE on each variable for BufferStrategy!

    Screen.java[HTML]<code>
    package zombified.gfx;

    import java.awt.Canvas;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.image.BufferStrategy;
    import java.awt.image.BufferedImage;

    import javax.swing.SwingUtilities;

    import zombified.Game;

    public class Screen extends Canvas {

    BufferedImage image = new BufferedImage(Game.WIDTH, Game.HEIGHT, BufferedImage.TYPE_INT_RGB);
    private Graphics2D g;
    private static final long serialVersionUID = 1L;
    private int ticks;
    private SpriteLoader loader = new SpriteLoader();

    public Screen() {
    for(int i = 0; loader.getSprite(i) != null; i += 0) {
    render(loader.getSprite(i).getImage(), loader.getSprite(i).getX(), loader.getSprite(i).getY());
    i++;
    }
    }

    public void tick() {
    int ticksPS = 60;
    if(ticks >= ticksPS) {
    ticks = 0;
    }
    ticks++;
    }

    public void render() {
    BufferStrategy bs = getBufferStrategy();
    if(bs == null) {
    createBufferStrategy(2);
    return; //returns and exits the method from this point to later be called again from the game lo op.
    }
    g = (Graphics2D)bs.getDrawGraphics();
    g.drawImage(image, 0, 0, null);
    g.dispose();
    bs.show();
    }

    //This is called by another class to render sprites and so forth to the screen.
    public void render(Image i, int x, int y) {
    g = image.getGraphics();
    //All g and bs calls below pass out null pointer exceptions!
    g.drawImage(i, x, y, null);
    g.dispose();
    bs.show();
    }

    }
    Last edited by Elijah; 01-01-2012 at 12:41 PM. Reason: This is the fix.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: NPE on each variable for BufferStrategy!

    From the API docs for getGraphics(), "This method will return null if this component is currently not displayable". And for getBufferStrategy(), "This method will return null if a BufferStrategy has not yet been created or has been disposed". (The API documentation should be your first port of call to see why methods return the values they do.)

    As far as getGraphics() is concerned, why do you want to call this? Normally whoever would want the sprites painted on the screen would have a valid Graphics instance and could just pass this to the render() methods.

    I've never used buffer strategies, but I note that you create it in the Screen constructor using the invokeLater() method so that it will take some time and it is quite possible that render() may be called before there has been a buffer strategy created.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: NPE on each variable for BufferStrategy!

    Also this thread is way off the beaten track. I'll move it to the Swing/AWT forum.

  4. #4
    Elijah is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default Re: NPE on each variable for BufferStrategy!

    Thanks for the help you guys I ended up watching Moderator edit:link removed realnotch - Ludum Dare 22. The theme is "Alone". Making a Harvest Moon game. and he explained all of it. Basically I think that my code was so screwed from messing around with it, all I needed was a simple

    <code>
    if(bs == null) {
    createBufferStrategy(2);
    return;
    }
    </code>

    Thanks if you read this and for helping me because this is the first time I have started a forum thread on my Java problems, I am 12 and it is really helpful to hear Java talk from another person, the syntax basically flies into my head when someone writes it and explains it in their own words so thanks for explaining is what I am trying to say I guess :P
    Last edited by DarrylBurke; 01-01-2012 at 04:10 PM. Reason: Link removed

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: NPE on each variable for BufferStrategy!

    I'm glad you've got it sorted out.

Similar Threads

  1. Copied code from this tutorial- BufferStrategy
    By JavaWizKid in forum New To Java
    Replies: 19
    Last Post: 11-14-2011, 08:06 PM
  2. Replies: 0
    Last Post: 10-08-2011, 05:45 PM
  3. BufferStrategy in JFrame
    By dbomb in forum AWT / Swing
    Replies: 3
    Last Post: 05-23-2011, 02:43 AM
  4. BufferStrategy
    By Shadowdude04 in forum AWT / Swing
    Replies: 0
    Last Post: 03-13-2010, 03:59 PM
  5. BufferStrategy and white rows of pixels
    By TobyLobster in forum Java 2D
    Replies: 2
    Last Post: 12-29-2008, 02:49 PM

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
  •