How to avoid this NullPointerException

前端 未结 3 902
礼貌的吻别
礼貌的吻别 2021-01-23 02:30

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 t

相关标签:
3条回答
  • 2021-01-23 02:44

    You're calling dbg on the this instance, not the instance of render.

    You need to change it to

    render.dbg.drawLine(....)
    

    Alternatively, if you wanted to leave the dbg call the same, you could call

    this.gameRender();
    

    first and then call

    dbg.drawLine(...);
    
    0 讨论(0)
  • 2021-01-23 02:48

    You setup dbg by invoking gameRender on render, but not this.

    0 讨论(0)
  • 2021-01-23 03:09

    Since MC extends render, the dbg you are referring to belongs to the MC instance that called draw. You can fix it by calling

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

    or take advantage of the inheritance you implemented

    class MC extends Render {
        //MC is a render, so you don't need to create another one
        public void draw() {
            gameRender(); //Call the MC's own gameRender
            dbg.drawLine( 100, 100, 200, 200 );  //Calling gameRender initialized dbg so you won't get a NullPointerException
        }
    }
    
    0 讨论(0)
提交回复
热议问题