Results 1 to 5 of 5
- 12-27-2013, 07:42 PM #1
Senior Member
- Join Date
- Jan 2013
- Posts
- 168
- Rep Power
- 9
I am getting a NullPointerException in my game and I don't know why?
So, I am getting a NullPointerException in the drawToScreen method when I try to draw the image... Does anyone know why?
Java Code:package com.patrickfeltes.game; import java.awt.Dimension; import java.awt.Graphics; import java.awt.image.BufferedImage; import javax.swing.JPanel; public class GamePanel extends JPanel implements Runnable { public static final int WIDTH = 400; public static final int HEIGHT = 300; public static final int SCALE = 2; public static boolean isRunning = false; private Thread thread; private static final int targetFPS = 60; private long targetTime = 1000 / targetFPS; private BufferedImage image; private Graphics g, g2; public GamePanel() { setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE)); start(); } public void start() { isRunning = true; thread = new Thread(this); thread.start(); } public void run() { image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); long start; long elapsed; long wait; while(isRunning) { start = System.nanoTime(); tick(); draw(); drawToScreen(); elapsed = System.nanoTime() - start; wait = targetTime - elapsed; if(wait < 0) { wait = 17; } try { Thread.sleep(wait); } catch (InterruptedException e) { e.printStackTrace(); } } } public void tick() { } public void draw() { g = image.getGraphics(); g.fillRect(10, 10, 10, 10); } public void drawToScreen() { g2 = getGraphics(); g2.drawImage(image, 0, 0, WIDTH * SCALE, HEIGHT * SCALE, null); } }
- 12-27-2013, 07:48 PM #2
Re: I am getting a NullPointerException in my game and I don't know why?
The stacktrace should tell you exactly which line it occurred. The only possible line that could cause this is line 81 in your example. So put a System.out.println() in there to see if g2 is null. If it is, find out why getGraphics() returns null. I think this code is run without the JPanel being visible.
"It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013
- 12-27-2013, 07:48 PM #3
Re: I am getting a NullPointerException in my game and I don't know why?
You forgot to paste the full text of the error message.
You get a NPE when trying to execute a statement with a variable with a null value.
Read the API doc for the NullPointerException class for a fuller explanation.If you don't understand my response, don't ignore it, ask a question.
- 12-27-2013, 07:50 PM #4
Senior Member
- Join Date
- Jan 2013
- Posts
- 168
- Rep Power
- 9
Re: I am getting a NullPointerException in my game and I don't know why?
I tried the System.out.println() and the first two statements were null, but after that it was fine. Should I initialize the g2 earlier?
- 12-27-2013, 09:22 PM #5
Similar Threads
-
Quick question about a game of Pig (Die game) that I'm writing Code for
By nwbaldi in forum New To JavaReplies: 4Last Post: 07-08-2012, 10:07 AM -
Complete Game Engine for beginner and intermediate game programmers
By rdjava in forum Java GamingReplies: 1Last Post: 06-02-2011, 10:29 AM -
Implementing "Game Over" in Minesweeper game based on Gridworld framework.
By JFlash in forum New To JavaReplies: 2Last Post: 08-05-2010, 05:49 AM -
game code for any game
By deathnote202 in forum Java GamingReplies: 4Last Post: 06-10-2010, 09:06 AM
Bookmarks