File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "NullPointerException" Watch "NullPointerException" New topic


nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 386

Why am I getting null pointer exception at line 10 ?
I am running this program on command line as "java ComparingTwoNumbers 3,5"
I have also tried "java ComparingTwoNumbers one , three", but same NullPointerException

Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

Isn't it line 11 that you get a NPE ? You have to separate your arguments by a space, so it should look like :

[My Blog]
All roads lead to JavaRanch
nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 386
I tried using "3 5" as you suggested, but still the same result, NPE at line 10

"java ComparingTwoNumbers 5 6" generates following output

"Exception in thread "main" java.lang.NullPointerException
at ComparingTwoNumbers.main("
RajTilak Sivaluri

Joined: Nov 13, 2010
Posts: 18
As you are trying to directly compare the values it will be good to use parseInt() method . Now if you replace getInteger() with parseInt it will work .

Just replace the code respective lines with following two lines

Happy coding !!!
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

That's not getInteger that you should be using, but valueOf.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15081

Indeed, Integer.getInteger() does something totally different than what you think. It is not parsing a string into an integer, but it's getting a system property with a specific name.

Instead of these two lines:

You want something like this:

(I'd use parseInt() and not valueOf() as Christophe suggests).

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Dhruva Mistry
Ranch Hand

Joined: Nov 21, 2008
Posts: 67

Hi Nirjari,

You need to convert the command line argument ie string into int using Integer wrapper class. And that can be done by


Determines the integer value of the system property with the specified name.
The first argument is treated as the name of a system property
and If there is no property with the specified name(ie parameter passed), if the specified name is empty or null, or if the property does not have the correct numeric format, then null is returned.

*this is found at Eclipse

I agree. Here's the link:
subject: NullPointerException
It's not a secret anymore!