Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Property problem: numberFormatException: null

0 views
Skip to first unread message

Gordon Hollenbeck

unread,
Mar 2, 2001, 7:31:27 PM3/2/01
to
Can anyone please tell me what I am doing wrong? I have created a temporary
method that saves Properties settings with the setProperty mehod and then
saved the file. I removed that temporary method. When I try to read the
file to get the dimensions for the JFrame, I receive the following
exception:

java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:373)
at java.lang.Integer.parseInt(Integer.java:454)
at phoenix.util.SettingsGetter.WinSizeGetter(SettingsGetter.java:44)
at phoenix.util.ScreenResolution.WindowSize(ScreenResolution.java:22)
at phoenix.client.main(client.java:91)
------------------------

Here is the method that gets the Dimension:

public static Dimension WinSizeGetter()
{
// the values for the Window parameters in Strings
String Swidth = prop.getProperty("WindowWidth");
String Sheight = prop.getProperty("WindowHeight");

// the values for the Window parameters in ints
int width = Integer.parseInt(Swidth);
int height = Integer.parseInt(Sheight);

Dimension window = new Dimension(width, height);

return window;
}
------------------------------
the Properties object is called prop, it is static and in the same class.

Can anyone help me??

--

Thanks,
Gordon Hollenbeck


Jon Skeet

unread,
Mar 3, 2001, 2:50:53 AM3/3/01
to
Gordon Hollenbeck <gor...@spamless.com> wrote:
> Here is the method that gets the Dimension:
>
> public static Dimension WinSizeGetter()
> {
> // the values for the Window parameters in Strings
> String Swidth = prop.getProperty("WindowWidth");
> String Sheight = prop.getProperty("WindowHeight");
>
> // the values for the Window parameters in ints
> int width = Integer.parseInt(Swidth);
> int height = Integer.parseInt(Sheight);
>
> Dimension window = new Dimension(width, height);
>
> return window;
> }
> ------------------------------
> the Properties object is called prop, it is static and in the same class.
>
> Can anyone help me??

Try printing out the values of Swidth and Sheight - chances are one of
them is null or not an integer. You should catch NumberFormatException
and either have some default values ready or complain to a higher level
if the values are invalid/missing.

Something like:

int width=DEFAULT_WIDTH; // Declared elsewhere as a constant
int height=DEFAULT_HEIGHT; // Declared elsewhere as a constant
try
{
width = Integer.parseInt (Swidth);
}
catch (NumberFormatException e)
{
// the default is already set
}
try
{
height = Integer.parseInt (Sheight);
}
catch (NumberFormatException e)
{
// the default is already set
}

Note that you need two separate try/catch blocks as otherwise if the
width is missing but the height isn't, the specified height would be
ignored. If you're just going to abort (eg by throwing an exception) if
either of them are missing/invalid, you don't need to do this, and you
can have something like:

int width=0; // The variables need scoping at this level
int height=0;
try
{
width = Integer.parseInt (Swidth);
height = Integer.parseInt (Sheight);
}
catch (NumberFormatException e)
{
// You'll have to write InvalidConfigurationException yourself!
throw new InvalidConfigurationException
("Missing or invalid width/height");
}


[btw, I notice from your stack trace and code that some of your
variables and many of your method names are capitalised. In this case
the code is still readable because there isn't much of it, but in
general it's worth at least considering Sun's code conventions,
available from
http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html
Most Java programmers stick to the naming conventions at least, even
if they disagree with the bracing style (as I do). It makes it easier to
distinguish methods, classes and variables.]


[Removed comp.lang.java - it hasn't been a valid newsgroup for a long
time. I'm not sure whether all the others are valid either, but I might
as well leave them in.]

--
Jon Skeet - sk...@pobox.com
http://www.pobox.com/~skeet

Gerhard Schwarz

unread,
Mar 5, 2001, 4:43:36 AM3/5/01
to
Hi Gordon,

Gordon Hollenbeck schrieb:


>
> Can anyone please tell me what I am doing wrong? I have created a temporary
> method that saves Properties settings with the setProperty mehod and then
> saved the file. I removed that temporary method. When I try to read the
> file to get the dimensions for the JFrame, I receive the following
> exception:
>
> java.lang.NumberFormatException: null

There you see, that you try to format null as a Number. That means
that Swidth (or Sheigth) is null, which means that one, or both,
Properties are not set.

> public static Dimension WinSizeGetter()
> {
> // the values for the Window parameters in Strings
> String Swidth = prop.getProperty("WindowWidth");
> String Sheight = prop.getProperty("WindowHeight");

> the Properties object is called prop, it is static and in the same class.

Is the Properties-Class initialized? You did not post that code, are
you sure, you load the Properties before you call WinSizeGetter?
You looked into the Propertiesfile and made shure that both Properties
exist?


Greets,
Gerhard

0 new messages