0

Below line of code gives NullPointerException in java

for any input (07,11 etc in src)

hrs = Integer.toString((Integer.getInteger(hrs)) + 12);

So i changed it to :

Integer  H = Integer.parseInt(hrs);
H = H + 12;
hrs = H.toString();

Which is working fine.

Can anyone tell me why first line is giving NullpointerException?

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140

5 Answers5

2

Simple: you are calling two different methods in your two examples.

Integer.getInteger(hrs)

Is not the same as

Integer.parseInt(hrs);

See the javadoc for getInteger():

Determines the integer value of the system property with the specified name. The first argument is treated as the name of a system property. System properties are accessible through the System.getProperty(java.lang.String) method. The string value of this property is then interpreted as an integer value and an Integer object representing this value is returned. Details of possible numeric formats can be found with the definition of getProperty.

If there is no property with the specified name, if the specified name is empty or null, or if the property does not have the correct numeric format, then null is returned.

Then null is returned!

That is all there is to this. So, the real answer is probably: pay more attention to the "code" suggestions that your IDE gives to you. Don't blindly accept them; but ensure that you are really putting down that method you intend to use; and not some other one that happens to have a similar signature (regarding input parameters and return types).

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248
1
 public static Integer getInteger(String nm)

Determines the integer value of the system property with the specified name. The first argument is treated as the name of a system property. System properties are accessible through the System.getProperty(java.lang.String) method. The string value of this property is then interpreted as an integer value and an Integer object representing this value is returned. Details of possible numeric formats can be found with the definition of getProperty.

If there is no property with the specified name, if the specified name is empty or null, or if the property does not have the correct numeric format, then null is returned.

In your case most probably you need to use

public static int parseInt(String s,
                           int radix)
                    throws NumberFormatException

For details of the both please use following link

Jabir
  • 2,776
  • 1
  • 22
  • 31
  • Man i did not notice it. I think we both were thinking on same line and you posted before me. I can delete mine if you say so. I dont have any issues with this... – Jabir Apr 13 '17 at 10:55
  • No worries; just wanted to understand. It simply feels strange to close the edit window and to get that "deja vu like" experience. And whatever; I just received my 100 vote on the string tag; so ... lets share the joy ;-) – GhostCat Apr 13 '17 at 11:17
0

You have to use Integer.parseInt(String) like this :

hrs = Integer.toString((Integer.parseInt(hrs)) + 12);

But you can use this instead :

hrs = (Integer.parseInt(hrs) + 12) + "";
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
0

A simple solution is

hrs = "" + (Integer.parseInt(hrs) + 12);
Md. Nasir Uddin Bhuiyan
  • 1,598
  • 1
  • 14
  • 24
0

Integer.getInteger() doesn't serve your purpose in this case. If you see the documentation of this method, it is mainly to determine the integer value of the system property with the specified name. If the given name does not matches with any of the system properties then it returns null.

In your code, (Integer.getInteger(hrs))+12 this is basically causing NPE as you are trying to concatenate null with 12

Please see this link for more details

Community
  • 1
  • 1
Sravya
  • 149
  • 5