Having trouble with the GUI?

Oct. 30, 2009

Posted by PERRY, JO under Information
0 Comments | Permalink

Some of you have complained about getting a Null Pointer Exception when trying to set a color on a "brush," like this:

Graphics brush = pnlSimulation.getGraphics();
brush.setColor(Color.BLUE);

That second statement will generate an error if you attempted to execute it before pnlSimulation is displayable. To correct the error, make sure that:

  • pnlSimulation is added to the content pane -- for example, with c.add(pnlSimulation, BorderLayout.CENTER())
  • the window is visible -- with this.setVisible(true);
Both of these conditions must be met before attempting to use the graphics for pnlSimulation.



Post a Comment:
Comments are closed for this entry.