Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do different Exceptions occur?

Tags:

java

I am using the below code. The first line is giving java.lang.NumberFormatException, and the second is giving java.lang.NullPointerException. I'm unable to figure out why.

int intValue =Integer.parseInt(null);
Double double1 = Double.parseDouble(null);
like image 235
Ajit Kumar Dubey Avatar asked Jan 09 '14 04:01

Ajit Kumar Dubey


Video Answer


1 Answers

Because thats how they are implemented,

int intValue =Integer.parseInt(null);

If we look the parseInt implementation, they are throwing NumberFormatException if the input string is null

enter image description here

And Double double1 = Double.parseDouble(null);

In parseDouble(String s) method there is another method call i.e FloatingDecimal.readJavaFormatString(s).doubleValue();
In readJavaFormatString(s) method is where exactly NullPointerException is thrown

enter image description here

FloatingDecimal.readJavaFormatString(s) method

enter image description here

like image 91
gowtham Avatar answered Sep 25 '22 23:09

gowtham