tag:blogger.com,1999:blog-9193160645817040317.post7200907788290606493..comments2015-12-22T12:31:53.234+05:30Comments on Twisters - The New Age Java Quiz: Puzzle 27 – To Null or Not to Null (Pointer Exception)Saifuddin Merchanthttp://www.blogger.com/profile/09008041357659535766noreply@blogger.comBlogger15125tag:blogger.com,1999:blog-9193160645817040317.post-30709464370476010672009-05-26T17:30:30.682+05:302009-05-26T17:30:30.682+05:30public static void SomeMethod(){}public static void SomeMethod(){}Akkihttps://www.blogger.com/profile/12899271203604845168noreply@blogger.comtag:blogger.com,1999:blog-9193160645817040317.post-41936436179379791102009-05-26T06:34:20.618+05:302009-05-26T06:34:20.618+05:30One thought is to add "static" to the declaration ...One thought is to add "static" to the declaration of SomeMethod(), letting the object reference fall through to the class method.<br /><br />I've not tried it yet, but I've often seen IDEA complain about calling the static method on an instance, so I'm hazarding a guess that fall through is not dependant on there being a valid instance object.Unknownhttps://www.blogger.com/profile/08099277416178856224noreply@blogger.comtag:blogger.com,1999:blog-9193160645817040317.post-84791592764822496592009-05-25T22:17:34.543+05:302009-05-25T22:17:34.543+05:30public class Exceptional {
public void So...public class Exceptional {<br /> <br /> public void SomeMethod(){}<br /><br /> public static void main(String[] args){<br /> Exceptional ex = new Exceptional();<br /> if(ex == null);<br /> ex.SomeMethod(); <br /> }<br />}stfnhttps://www.blogger.com/profile/13802280639197311708noreply@blogger.comtag:blogger.com,1999:blog-9193160645817040317.post-2404918937597567352009-05-25T18:19:52.045+05:302009-05-25T18:19:52.045+05:30For this thing to work, we just have to make sure ...For this thing to work, we just have to make sure that the ex=null; never runs. So, if we do something like<br /><br />public static void main(){<br /> Exceptional ex = new Exceptional();<br /> if (1!=1) <br /> ex = null;<br /> ex.SomeMethod(); //This must not throw an exception!!<br /> }<br />}<br /><br />ex will never be null, therefore, no NullPointerException will be thrown.<br /><br />Cheers, Bernardobernardomenhttps://www.blogger.com/profile/05958656847396083327noreply@blogger.comtag:blogger.com,1999:blog-9193160645817040317.post-68464684036476469802009-05-25T17:11:27.146+05:302009-05-25T17:11:27.146+05:30BTW, really a nice blog. Gonna read :)BTW, really a nice blog. Gonna read :)TheMalkolmhttps://www.blogger.com/profile/11358745568436186926noreply@blogger.comtag:blogger.com,1999:blog-9193160645817040317.post-65113019550360797782009-05-25T17:09:41.512+05:302009-05-25T17:09:41.512+05:30Really dont know rules for your game, but maybe th...Really dont know rules for your game, but maybe that way?<br /><br /> 1 package com.twisters;<br /> 2 public class Exceptional{<br /> 3 <br /> 4 public static void SomeMethod(){} <br /> 5 <br /> 6 public static void main(){<br /> 7 Exceptional ex = new Exceptional();<br /> 8 ex = null;<br /> 9 ex.SomeMethod(); //This must not throw an exception!!<br />10 }<br />11 }TheMalkolmhttps://www.blogger.com/profile/11358745568436186926noreply@blogger.comtag:blogger.com,1999:blog-9193160645817040317.post-24371803093036683062009-05-25T16:48:15.484+05:302009-05-25T16:48:15.484+05:30ex = null;
if(ex==null)ex=new Exceptional();
ex.So...ex = null;<br />if(ex==null)ex=new Exceptional();<br />ex.SomeMethod();kannanhttps://www.blogger.com/profile/03791870748411766033noreply@blogger.comtag:blogger.com,1999:blog-9193160645817040317.post-91268400541482125632009-05-25T15:40:21.933+05:302009-05-25T15:40:21.933+05:30ok.. the "make it tough" does nothing for me :P
s...ok.. the "make it tough" does nothing for me :P<br /><br />since my changes were between line 7 and 8 (and not 8 and 9) the solution i posted still applies.<br /><br />no ex=null, so, no exception.Epikkhttps://www.blogger.com/profile/16959891098899793242noreply@blogger.comtag:blogger.com,1999:blog-9193160645817040317.post-30963385761490098542009-05-25T15:39:20.369+05:302009-05-25T15:39:20.369+05:30my solution is based on not assigning the null val...my solution is based on not assigning the null value to ex.<br /><br />so basically, i added an "if (false)" statement between lines 7 and 8, so that the null attribution never happens, making it run without exceptions :)<br /><br />I'm going to go for the "make it hard" now to see if it still applies<br /><br />package com.twisters;<br /> public class Exceptional{<br /> <br /> public void SomeMethod(){} <br /> <br /> public static void main(){<br /> Exceptional ex = new Exceptional();<br /> if (false)<br /> ex = null;<br /> ex.SomeMethod(); //This must not throw an exception!!<br /> }<br /> }Epikkhttps://www.blogger.com/profile/16959891098899793242noreply@blogger.comtag:blogger.com,1999:blog-9193160645817040317.post-58183050698530861632009-05-25T03:14:29.157+05:302009-05-25T03:14:29.157+05:30Ah my earlier solution holds true for the tougher ...Ah my earlier solution holds true for the tougher side too! yay!Abhihttp://www.google.comnoreply@blogger.comtag:blogger.com,1999:blog-9193160645817040317.post-35413417590322842862009-05-25T03:13:39.989+05:302009-05-25T03:13:39.989+05:30Trying to answer the non-made tough part. First th...Trying to answer the non-made tough part. First the program will not work as a proper main method is missing. Assuming thats just a misprint - <br /><br />Replace, <br />public void SomeMethod(){}; <br />with,<br />public static void SomeMethod(){};<br /><br />Easy!Abhihttp://www.google.comnoreply@blogger.comtag:blogger.com,1999:blog-9193160645817040317.post-55968295348966718522009-05-25T00:18:48.268+05:302009-05-25T00:18:48.268+05:30public static void doSomething(){}
and making pub...public static void doSomething(){}<br /><br />and making public static void main()<br />public static void main(String [] args)<br /><br />PS. my Open id doesn't seem to be working perfectly yet it should be syfranAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-9193160645817040317.post-89032949658040052052009-05-25T00:12:34.328+05:302009-05-25T00:12:34.328+05:30Ho and I forgot :
public static void SomeMethod() ...Ho and I forgot :<br />public static void SomeMethod() {}Makkhdynhttps://www.blogger.com/profile/16225036210991551541noreply@blogger.comtag:blogger.com,1999:blog-9193160645817040317.post-34258825338000141072009-05-25T00:07:22.167+05:302009-05-25T00:07:22.167+05:30public static void main() throws Throwablepublic static void main() throws ThrowableMakkhdynhttps://www.blogger.com/profile/16225036210991551541noreply@blogger.comtag:blogger.com,1999:blog-9193160645817040317.post-7443404237953358812009-05-25T00:05:02.116+05:302009-05-25T00:05:02.116+05:30Nice twister, this should work: replace line 4 wit...Nice twister, this should work: replace line 4 with<br /><br />public static void SomeMethod(){}<br /><br />regards, JörnAnonymousnoreply@blogger.com