• 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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?
 
Grow a forest with seedballs and this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic