How to avoid this NullPointerException

戏子无情 提交于 2019-12-02 01:30:51

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(...);

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

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
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!