-1

Possible Duplicate:
How to avoid this NullPointerException

I actually asked this question earlier, but I had to edit it, and I think I was too late because nobody answered after that point.

I'm working on a small arcade video game, and I am looking to double buffer to improve animation. I have one class that's supposed to draw the blank image, and another class that's supposed to draw a simple line. However, I keep getting a NullPointerException on the line where the line is supposed to be drawn

class Render extends JPanel {
    public int dbWidth = 500, dbHeight = 400;
    public Image dbImage = null;
    public Graphics dbg;

    public void gameRender() {

        if( dbImage == null )  
            dbImage = createImage( dbWidth, dbHeight );

        dbg = dbImage.getGraphics();
        dbg.setColor( Color.white );
        dbg.fillRect( 0, 0, dbWidth, dbHeight );
    }
}

class MC extends Render {
    public Render render = new Render();

    public void draw() {
        render.gameRender();
        dbg.drawLine( 100, 100, 200, 200 ); // line where NullPointerException occurs
    }
}

I suppose it's the Graphics variable dbg that's null, but it gets the value of dbImage.getGraphics(); in gameRender(); How could I fix this NullPointerException?

I am also calling the draw() method in another class like this

    public void run() {

    running = true;

    while( running ) {

        mc.draw();
        try {
            Thread.sleep( 50 );
        }
        catch( Exception e ) {}
    }
}

I said in that class's constructor that mc = new MC();

Community
  • 1
  • 1
kullalok
  • 813
  • 2
  • 13
  • 19
  • You've gotten good answers on your question already. Why ask another? – gobernador Jun 29 '12 at 14:36
  • 1
    http://stackoverflow.com/questions/11263199/how-to-avoid-this-nullpointerexception – kullalok Jun 29 '12 at 14:36
  • It didn't work. That's why I'm asking it again because I feel like I didn't add a crucial component to the question. – kullalok Jun 29 '12 at 14:36
  • 1
    There's no need to ask a new question. Keep it on that same thread. Post comments asking the answerers for more information. – gobernador Jun 29 '12 at 14:38
  • The line where this fails or a stacktrace would be most helpful. – WhyNotHugo Jun 29 '12 at 14:47
  • As the others have said here, the correct way to address shortcomings in the answers that have been provided to your older questions is not to re-ask them. Instead, leave comments on the answers explaining why they didn't solve your problem and / or edit the original question to add additional information or clarify what you're seeing. I know that you're anxious to resolve these problems, but multiple questions on the same topic can try the patience of the volunteers answering them. – Brad Larson Jul 02 '12 at 00:40

3 Answers3

3

class MC extends class Render. That means MC inherits Render's dbg field. You don't need to create another instance of Render. Try this instead:

class MC extends Render {

    public void draw() {
        super.gameRender();
        dbg.drawLine( 100, 100, 200, 200 ); // line where NullPointerException occurs
    }
}
Dan O
  • 6,022
  • 2
  • 32
  • 50
1

You're dealing with two instances of Render here: there is a render field in your MC class, and since MC itself extends Render, there is another implicit instance there. When you do:

render.gameRender();

you are calling gameRender on the instance, which initializes render.dbg, but then when you call dbg.drawLine you are using MCs instance of dbg which is not yet initialized.

In this case, you really don't need inheritance, so just the render field will do:

public void draw() {
    render.gameRender();
    render.dbg.drawLine( 100, 100, 200, 200 );
}

Also, it's not good practice to make fields public, so consider adding a getter for dbg in Render:

public Graphics getGraphics() {
    return dbg;
}

...

render.getGraphics().drawLine( 100, 100, 200, 200 );

or delegate the drawLine call:

public void drawLine(int x1, int y1, int x2, int y2) {
    dbg.drawLine(x1, y1, x2, y2);
}

...

render.drawLine( 100, 100, 200, 200 );
casablanca
  • 69,683
  • 7
  • 133
  • 150
0

I suppose your draw method gets called by the java drawing environment before the constructor gets called.

Place one breakpoint inside the constructor and one breakpoint inside the draw() to verify this.

If thats true simply surround the draw statement with an if to check != null. Of course it's a workaround but maybe it helps you.

marc wellman
  • 5,808
  • 5
  • 32
  • 59