This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
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
Author

NullPointerException

nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 374

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

Thanks
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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: 374
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(ComparingTwoNumbers.java:10)"
RajTilak Sivaluri
Greenhorn

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é
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Aug 16, 2005
Posts: 14194
    
  20

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



because,


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


Dhruva
 
GeeCON Prague 2014
 
subject: NullPointerException