• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NullPointerException

 
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)"
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's not getInteger that you should be using, but valueOf.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 67
Mac Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic