Results 1 to 5 of 5
  1. #1
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    160
    Rep Power
    2

    Default 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);
    	}
    	
    	
    	
    }

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    753
    Rep Power
    2

    Default 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

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,608
    Rep Power
    23

    Default 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.

  4. #4
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    160
    Rep Power
    2

    Default 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?

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,608
    Rep Power
    23

    Default Re: I am getting a NullPointerException in my game and I don't know why?

    Why not override the paintComponent() method and use the Graphics object passed to it to do the drawing?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 4
    Last Post: 07-08-2012, 09:07 AM
  2. Replies: 1
    Last Post: 06-02-2011, 09:29 AM
  3. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  4. game code for any game
    By deathnote202 in forum Java Gaming
    Replies: 4
    Last Post: 06-10-2010, 08:06 AM

Posting Permissions

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