Java Problem

Mavix

Senior Member
Joined
Aug 14, 2006
Messages
759
Reaction score
3
Location
Durban
I'm having a bit of a problem with Java, and it's starting to become rather unpleasant.
In general I hate (Like really, *hate*) Java (I prefer C#), but I have to do it at school.
Now can anyone see *any* problem with the code below?

Code:
try
{
    int amount = Integer.getInteger("150");
    System.out.println("You entered "+amount);
}
catch (Exception e)
{
    System.out.println("Exception: "+e.toString());
}
Because it keeps printing "Exception: java.lang.NullPointerException", and I cannot for the life of me figure out why.
Any ideas?

BTW, if it makes any difference, I'm running Java 1.6, Netbeans 6.7, Windows 7.
 
NullPointerException AFAIK means the same thing as Object Not Referenced or Null
 
I'm having a bit of a problem with Java, and it's starting to become rather unpleasant.
In general I hate (Like really, *hate*) Java (I prefer C#), but I have to do it at school.
Now can anyone see *any* problem with the code below?

Code:
try
{
    int amount = Integer.getInteger("150");
    System.out.println("You entered "+amount);
}
catch (Exception e)
{
    System.out.println("Exception: "+e.toString());
}
Because it keeps printing "Exception: java.lang.NullPointerException", and I cannot for the life of me figure out why.
Any ideas?

BTW, if it makes any difference, I'm running Java 1.6, Netbeans 6.7, Windows 7.

Why are you using getInteger()? Surely you should be using getValue()?

getInteger() assumes the String is the name of a system property numeric representation, and in your case returns null. Thus, the exception.
 
Why are you using getInteger()? Surely you should be using getValue()?

getInteger() assumes the String is the name of a system property numeric representation, and in your case returns null. Thus, the exception.

Err... getValue()? :confused:
 
I'm having a bit of a problem with Java, and it's starting to become rather unpleasant.
In general I hate (Like really, *hate*) Java (I prefer C#), but I have to do it at school.

You do know that C# is based on Java? In fact many Java code snippets can work in C# without any changes.
 
You do know that C# is based on Java? In fact many Java code snippets can work in C# without any changes.

Not surprised. Isn't actually based on Microsoft's failed version of Java, which is based on Java? Anyways, I find Java to be excessively slow, and the IDE's (Netbeans at least, haven't tried Eclipse) suck. But that's just my opinion...
 
Integer.getValue("1234") or Integer.parseInt("1234") would work in your case.

Integer.getInteger("1234") is not correct for what you are trying to do.

Of course! *Facepalm*. My bad...
Thanks for the help!
 
Top
Sign up to the MyBroadband newsletter
X