Advertisement Jump to content
Sign in to follow this  

Graphics Context Error

This topic is 2490 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I cannot for the life of me, figure out why this code doesn't work.
I keep getting two errors that I defined in try catch statements: dbImage is null and Graphics Context Error: java.lang.NullPointerException
These appear every frame.
I know this is a lot of code, but I want to include anything that could affect it. I'm also leaving out a lot of functions that work with the running of the game not the rendering.

Anyways, the errors are coming from the BufferImage and RenderImage functions

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
import com.sun.j3d.utils.timer.J3DTimer;
public class PlatformEngine extends JPanel implements Runnable{
//Window Variables
public static int PWIDTH = 520;
public static int PHEIGHT = 400;
public static PlatformEngine game = new PlatformEngine();

//Method Specific Variables
public boolean running = false;
public Thread th = new Thread(this);
public int cycleTime = 15;
public int MAX_FRAME_SKIPS = 5;

public Player player = new Player();

private Graphics dbg;
private Image dbImage = null;
public PlatformEngine(){
setPreferredSize(new Dimension(PWIDTH,PHEIGHT));

//Start game thread

//Game Functions
public void run(){
//Updates, Buffers, and Renders the game each frame
//These variables are used to standardize the framerate
long beforeTime,afterTime,timeDiff,sleepTime;
long overSleepTime = 0L;
long excess = 0L;
int skips = 0;

beforeTime = J3DTimer.getValue();

running = true;

afterTime = J3DTimer.getValue();
timeDiff = afterTime - beforeTime;
sleepTime = cycleTime - timeDiff;

excess -= sleepTime;

}catch(Exception e){}
while((excess > cycleTime) && (skips<MAX_FRAME_SKIPS)){
UpdateGame(); //Update but don't render

public void UpdateGame(){
//Updates the game
/* This involves:
- checking and handling the active status of entities
- checking collisions

public void BufferImage(){
if(dbImage == null){
dbImage = createImage(PWIDTH,PHEIGHT);
if(dbImage == null){
System.out.println("dbImage is null");
dbg = dbImage.getGraphics();
//clear the background
//Individual Paint Functions Here
/* These most likely need to check the state first
REMINDER: Implement State Machine before going into all that


public void RenderImage(){
//Draws a pre rendered image of the game to the Panel
Graphics g;
g = this.getGraphics();


}catch(Exception e){
System.out.println("Graphics Context Error: " + e);

Any Help would be great,

Share this post

Link to post
Share on other sites
Ok, nobody seems to be able to find a solution to this. I've asked at school too, and neither my teacher or my classmates can figure this out. Really, what I'm asking is: How would I create my own method to draw a picture, this picture being the next frame (double buffer) and then without using JPanel's paintComponent() method, how would I paint that to the JPanel.

The reason behind the last part is that the call to repaint() is only a request that can be handled whenever the processor has time. I want to ensure that when I tell the JPanel to repaint, that it repaints right then.

Any help is greatly appreciated,

Thanks in advance,

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!