Win a copy of Learning Java by Building Android Games this week in the Android forum!
  • 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
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

getGraphics() returns null  RSS feed

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The getGraphics() method in my constructor is returning a nullPointerException and I can't figure out why. This class is extending a JPanel and I thought calling super() would fix it because it makes sense that the JPanel would initialize its graphics object in the constructor, but that didn't fix it. The line that produces the error is in bold. I've narrowed it down to the getGraphics() method is returning null, so the next line that calls the graphics object is actually the one that produces the error.

Can someone please help me figure out why this is returning null?



P.S. print(String) is a method I made that draws lines of text on the graphics object, similar to System.out.println(String) in the command prompt.

[ August 23, 2007: Message edited by: Derek Boring ]
[ August 23, 2007: Message edited by: Derek Boring ]
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Graphics object will be null until something is visible. This is demonstrated below.

(Note: If there is no explicit call to this or super as the first line of a constructor, then there is an implicit call to super with no arguments. So explicitly adding super() should not make a difference.)
 
Derek Boring
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The HTMLFrame is shown inside a JScrollPane which is inside a JTabbedPane which is on a JFrame that is visible. (hope that made sense) The JFrame shows, but nothing shows on it and I still get a null pointer exception at the same place. The JFrame is shown before anything is added to it.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Derek Boring:
The HTMLFrame is shown inside a JScrollPane which is inside a JTabbedPane which is on a JFrame that is visible. (hope that made sense) The JFrame shows, but nothing shows on it and I still get a null pointer exception at the same place. The JFrame is shown before anything is added to it.


I think the JPanel itself (the HTMLFrame) needs to be visible at the time getGraphics is called.
 
Derek Boring
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found the problem, thanks for the help.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Derek what was the problem ? could you please share it i am facing the same
 
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Neeraj jain wrote:@Derek what was the problem ? could you please share it i am facing the same



If by 'the same' you mean that getGraphics(0 of a Component is null, the solution is simple: don't use getGraphic(). Instead, do custom painting in the recommended way: http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html
 
Marshal
Posts: 60737
189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this discussion would fit better in our GUIs forum: moving.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!