Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointerException

 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't it line 11 that you get a NPE ? You have to separate your arguments by a space, so it should look like :
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not getInteger that you should be using, but valueOf.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15208
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Dhruva Mistry
Ranch Hand
Posts: 67
Eclipse IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic