Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1048 :
Posted: Mon Oct 01, 2012 9:24 am
by ETS User
Why throw NullPointerException whe RUn?
Tank you
Re: About Question enthuware.ocajp.i.v7.2.1048 :
Posted: Mon Oct 01, 2012 10:16 am
by admin
NullPointerException is thrown by the statement throw re; because re is null. This will be caught by the catch block, which prints the exception.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1048 :
Posted: Thu Aug 29, 2013 11:41 am
by adrian110288
admin wrote:NullPointerException is thrown by the statement throw re; because re is null. This will be caught by the catch block, which prints the exception.
HTH,
Paul.
Still dnt quite understand it :/ Is NullPointerException caught, because the try block throws null value? I thought the NullPointerException is thrown when we try to access an objec which is null, but here we dnt try to access the value thrown by try block, do we? im confused ...
Re: About Question enthuware.ocajp.i.v7.2.1048 :
Posted: Thu Aug 29, 2013 1:52 pm
by admin
Yes, the NPE is thrown because of the statement throw re, where re is null.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1048 :
Posted: Fri Jul 03, 2015 9:15 am
by Ad9999
throw null; = throw new NullPointerException();
Cool. Null is so versatile.
Re: About Question enthuware.ocajp.i.v7.2.1048 :
Posted: Wed Sep 06, 2017 12:19 pm
by gianmarcoelg
You could found inside the NullPointerException class the following:
*Throwing {@code null} as if it were a {@code Throwable} value.
Regards.
gmeg
Re: About Question enthuware.ocajp.i.v7.2.1048 :
Posted: Tue Jul 10, 2018 10:31 am
by __JJ__
Hi
A NullPointerException will be thrown if the expression given to the throw statement results in a null pointer.
This is very interesting. I had thought the throw would throw a null object which would nonetheless be caught by the catch but that the attempt to print it in the SOP (calling e.toString() maybe) would cause an NPE.
I had to verify it for myself. To drive the point home:
Code: Select all
try{
IOException e = null;
throw e;
}
catch(IOException e){
System.out.println("UCK IOE");
System.out.println(e);
}
catch(NullPointerException e){
System.out.println("UCK NPE");
System.out.println(e);
}
catch(IllegalArgumentException e){
System.out.println("UCK IAE");
System.out.println(e);
}
catch(RuntimeException e){
System.out.println("UCK RTE");
System.out.println(e);
}
prints
Code: Select all
UCK NPE
java.lang.NullPointerException
Re: About Question enthuware.ocajp.i.v7.2.1048 :
Posted: Sun Feb 17, 2019 3:21 pm
by MariaRoxana
Hy,
Can someone, please, explain to me, why System.out.println(e) outputs java.lang.RuntimeException? I remember that println(Object o) will do String.valueOf(x) to get the printed object's string value but x is null, so it prints null because of print(String x). I tried to read the documentation but I didn't understand very well the idea.
Thx

Re: About Question enthuware.ocajp.i.v7.2.1048 :
Posted: Sun Feb 17, 2019 9:31 pm
by admin
You remember correctly that if the argument to SOP is null, then null is printed. However, in the given code, e is not null. The statement "throw re;" in the try block does not send a null reference to the catch block. Instead, this statement actually causes a NullPointerException. That is why, the e in catch(Exception e) does not get a null but a reference to a valid NullPointerException object, which is what is printed by the SOP statement.