• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

NullPointerException on g.drawString

 
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.
 
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?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!