Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointerException on g.drawString

 
sonny kher
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a start method that looks something like this
void start(){

LayeredPane lp = new JLayeredPane();
lp.setvisible(true);
add(lp)
Graphics g = lp.getGraphics();
g.drawString("check", 20, 20);
}

This throws a NullPointerException, any action on lp.getGraphics returns a NullPointerException. I don't know why this is happening.
If I put the drawString inside paint(Graphics g)
everything else disappears and only the String shows.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect that you're trying to get a Graphics object from something that isn't actually being displayed before you get the Graphics object (but it's hard to tell exactly what you're doing as the code that you've posted has errors and is out of context). Consider:which prints null and which doesn't print null. Getting any ideas?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic