Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

getting a null pointer when trying to create a new font

Thread view: 
Justin - 12 Oct 2006 00:07 GMT
I have a class that extends a JDialog component.  Within the
constructor, I call an addWidgets method.  That method contains the
line in question:

Font nameFont = getFont().deriveFont(1,30);

This line is causing a null pointer exception.  I dont understand why,
if someone could help me create the new font and explain to me why this
causes an error, it would be greatly appreciated.

Also the import list is as follows:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.font.*;

Thanks
Thomas Weidenfeller - 12 Oct 2006 08:13 GMT
> I have a class that extends a JDialog component.  Within the
> constructor, I call an addWidgets method.  That method contains the
[quoted text clipped - 3 lines]
>
> This line is causing a null pointer exception.  I dont understand why,

A component which hasn't been realized does this. It's the way AWT and
Swing work.

/Thomas
Signature

The comp.lang.java.gui FAQ:
http://gd.tuwien.ac.at/faqs/faqs-hierarchy/comp/comp.lang.java.gui/
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq

Justin - 12 Oct 2006 16:00 GMT
Ok, but did I not initialize it on the line:

Font nameFont = getFont().deriveFont(1,30);

???
Tor Iver Wilhelmsen - 12 Oct 2006 18:40 GMT
> Ok, but did I not initialize it on the line:
>
> Font nameFont = getFont().deriveFont(1,30);

getFont() requires a native peer, and that won't exist until the
component has been shown.
Joe Attardi - 12 Oct 2006 18:46 GMT
> > Ok, but did I not initialize it on the line:
>
> > Font nameFont = getFont().deriveFont(1,30);getFont() requires a native peer, and that won't exist until the
> component has been shown.

Justin,
This is why code like that can be troublesome. If getFont() returns
null, as it is in this case, in your code there's no way to stop the
call to deriveFont(1, 30) and thus your NullPointerException.

While more verbose, something like this is a little more
error-friendly:

Font nameFont = getFont();
if (nameFont != null)
   nameFont = nameFont.deriveFont(1, 30);

The burden is on you to then provide graceful handling of this error
condition, but at least your user won't see a NullPointerException...

Signature

Joe Attardi
jattardi@gmail.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.