7

my application looks like that, i am getting a null pointer exception at the draw() method, to be exact at g.drawImage(img, 0, 0, null)

package com.ochs.game;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Game extends JPanel implements Runnable{
private static final long serialVersionUID = 8229934361462702491L;

public static final int WIDTH = 320;
public static final int HEIGHT = 240;
public static final int SCALE = 2;

public boolean isRunning;

private BufferedImage img;
private Graphics2D g2d;

public Game() {
    setFocusable(true);
    requestFocus();
    start();
}

public void start() {
    isRunning = true;
    new Thread(this).start();
}

public void stop() {
    isRunning = false;
}

public void run() {
    long start;
    init();
    while(isRunning) {
        start = System.currentTimeMillis();

        update();
        render();
        draw();

        try {
            Thread.sleep(5 - (System.currentTimeMillis() - start));
        } catch (Exception e) {
        }
    }
}

public void init() {
    img = new BufferedImage(WIDTH*SCALE, HEIGHT*SCALE, BufferedImage.TYPE_INT_RGB);
    g2d = (Graphics2D) img.getGraphics();
}

public void update() {

}

public void render() {

}

public void draw() {
    Graphics g = getGraphics();
    g.drawImage(img, 0, 0, null);    // <<<<< getting null pointer here!
}

public static void main(String[] args) {
    Dimension size = new Dimension(WIDTH*SCALE, HEIGHT*SCALE);
    Game gameComponent = new Game();
    JFrame frame = new JFrame();
    frame.setVisible(true);
    frame.setSize(size);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(gameComponent);
}
}

Now my question is: why do i get a null pointer exception when trying to draw the bufferedimage called img? I also tried just outputting some string by using drawString() but this just gives myself a nullpointerexception, too. does anyone has an advice?

2
  • How about including the exception? What is null? g? img? Commented Aug 11, 2013 at 12:22
  • I guess your graphics object is null since you start drawing the image (from that thread started in constructor) before you added the component to it's frame. Commented Aug 11, 2013 at 12:25

4 Answers 4

7

You're likely trying to get the Graphics context via getGraphics() before the JPanel has been rendered, and thus the method returns null. Don't do this. There are problems with using getGraphics() on a component to get the Graphics context, one of which is the problem you're seeing above, and another is that the Graphics context obtained will not persist. There are occasions when this is necessary to do, but usually we do passive drawing via paintComponent(...). Often a Swing Timer can be used for the animation loop.

Sign up to request clarification or add additional context in comments.

Comments

3

I think it's because you're trying to draw using getGraphics() instead of the conventional override of paintComponent. You want to use something like this: drawImage is not drawing (see the top answer).

Comments

0

the Component must first be visible try this before you start the thread in Game/panel

frame.add(panel) frame.setVisible(true)

then start the thread in Game/panel

Comments

0

getGraphics() method will return null if Component is not rendered till that statement and thus you will get NullPointerException, also if it is rendered Graphics will not be stable and better to use a paintComponents...

See also: Any alternative to calling getGraphics() which is returning null

import java.awt.*;
import java.awt.event.*;

class myframe extends Panel
{
    public void paint(Graphics g)
    {
        g.setColor(Color.red);
        g.fillRect(10,12,300,150);
    }
    
    public static void main(String args[])
    {
        Frame f=new Frame();
        f.add(new myframe());
        
        
        f.setSize(400,400);
        f.setVisible(true);
        
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.