Dereferencing possible null pointer in java

Posted by Nealio on Stack Overflow See other posts from Stack Overflow or by Nealio
Published on 2013-10-31T03:51:51Z Indexed on 2013/10/31 3:53 UTC
Read the original article Hit count: 135

Filed under:

I am just starting to get into graphics and when I am trying to get the graphics, I get the error"Exception in thread "Thread-2" java.lang.NullPointerException" and I have no clue on what is going on! Any help is greatly appreciated.

//The display class for the game
//Crated: 10-30-2013
//Last Modified: 10-30-2013
package gamedev;

import gamedev.Graphics.Render;
import gamedev.Graphics.Screen;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.JFrame;


private void tick() {
}

private void render() {
    System.out.println("display.render");
    BufferStrategy bs = this.getBufferStrategy();
    if (bs == null) {
        createBufferStrategy(3);
    }

    for (int i = 0; i < GAMEWIDTH * GAMEHEIGHT; i++) {
        pixels[i] = screen.PIXELS[i];
    }

    screen.Render();

//The line of code that is the problem
    Graphics g = bs.getDrawGraphics();
//end problematic code

    g.drawImage(img, 0, 0, GAMEWIDTH, GAMEHEIGHT, null);
    g.dispose();
    bs.show();
}

© Stack Overflow or respective owner

Related posts about java