java - How to avoid this NullPointerException -


i'm working on small arcade video game, , looking double buffer improve animation. have 1 class that's supposed draw blank image, , class that's supposed draw simple line. however, keep getting nullpointerexception on line line supposed 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 nullpointerexception occurs     } } 

i suppose it's graphics variable dbg that's null, gets value of dbimage.getgraphics(); in gamerender(); how fix nullpointerexception?

i calling draw() method in class this

    public void run() {      running = true;      while( running ) {          mc.draw();         try {             thread.sleep( 50 );         }         catch( exception e ) {}     } } 

i said in class's constructor mc = new mc();

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

you need change to

render.dbg.drawline(....) 

alternatively, if wanted leave dbg call same, call

this.gamerender(); 

first , call

dbg.drawline(...); 

Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -