The format of the puzzle – make it very easy at times to answer a puzzle without really giving it a lot of though. It put me in a dilemma – should I allow the puzzle to be simple or make them tough right from the beginning. I would love to make that decision – but I found someone better to make this decision – You.
Oh don't worry it's not an opinion asking post. It's just a small change in how things work. Let me explain – The puzzles would be in the same format as they were before. You will also have an extra choice – answer it the way it is or hit what I call the 'make it tough.' link. Voila the same puzzle but a little tougher now!!!
My suggestion – Try and answer the puzzle without hitting the 'make it tough' link – just to see what solution you come up with at the top of your mind. Post it – Really I love to hear from you folks. Hit the 'make it tough' link and see if your solution still works. You can always re-post a new solution anytime!!
Enough of my chatter – puzzle time!
package com.twisters;
public class Exceptional{
public void SomeMethod(){}
public static void main(){
Exceptional ex = new Exceptional();
ex = null;
ex.SomeMethod(); //This must not throw an exception!!
}
}
Your code must not throw a null pointer exception.
You may add (but not delete) any additional amount of code – and no you are not allowed to handle the exception using try-catch. Make it tough?
No characters allowed between the 8th and 9th line. So nothing comes between the ex=null and ex.SomeMethod().
Site for the day - http://www.theproblemsite.com/treasure_hunt/ - Try your luck out there treasure hunters!
Got an answer? Do leave it here.
Nice twister, this should work: replace line 4 with
ReplyDeletepublic static void SomeMethod(){}
regards, Jörn
public static void main() throws Throwable
ReplyDeleteHo and I forgot :
ReplyDeletepublic static void SomeMethod() {}
public static void doSomething(){}
ReplyDeleteand making public static void main()
public static void main(String [] args)
PS. my Open id doesn't seem to be working perfectly yet it should be syfran
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 -
ReplyDeleteReplace,
public void SomeMethod(){};
with,
public static void SomeMethod(){};
Easy!
Ah my earlier solution holds true for the tougher side too! yay!
ReplyDeletemy solution is based on not assigning the null value to ex.
ReplyDeleteso basically, i added an "if (false)" statement between lines 7 and 8, so that the null attribution never happens, making it run without exceptions :)
I'm going to go for the "make it hard" now to see if it still applies
package com.twisters;
public class Exceptional{
public void SomeMethod(){}
public static void main(){
Exceptional ex = new Exceptional();
if (false)
ex = null;
ex.SomeMethod(); //This must not throw an exception!!
}
}
ok.. the "make it tough" does nothing for me :P
ReplyDeletesince my changes were between line 7 and 8 (and not 8 and 9) the solution i posted still applies.
no ex=null, so, no exception.
ex = null;
ReplyDeleteif(ex==null)ex=new Exceptional();
ex.SomeMethod();
Really dont know rules for your game, but maybe that way?
ReplyDelete1 package com.twisters;
2 public class Exceptional{
3
4 public static void SomeMethod(){}
5
6 public static void main(){
7 Exceptional ex = new Exceptional();
8 ex = null;
9 ex.SomeMethod(); //This must not throw an exception!!
10 }
11 }
BTW, really a nice blog. Gonna read :)
ReplyDeleteFor this thing to work, we just have to make sure that the ex=null; never runs. So, if we do something like
ReplyDeletepublic static void main(){
Exceptional ex = new Exceptional();
if (1!=1)
ex = null;
ex.SomeMethod(); //This must not throw an exception!!
}
}
ex will never be null, therefore, no NullPointerException will be thrown.
Cheers, Bernardo
public class Exceptional {
ReplyDeletepublic void SomeMethod(){}
public static void main(String[] args){
Exceptional ex = new Exceptional();
if(ex == null);
ex.SomeMethod();
}
}
One thought is to add "static" to the declaration of SomeMethod(), letting the object reference fall through to the class method.
ReplyDeleteI'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.
public static void SomeMethod(){}
ReplyDelete