Sunday, May 24, 2009

Puzzle 27 – To Null or Not to Null (Pointer Exception)

Language – Java | Type – Concept | Last date 27-May-2009 9:00 p.m. IST | Points 3

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?

Site for the day - http://www.theproblemsite.com/treasure_hunt/ - Try your luck out there treasure hunters!

Got an answer? Do leave it here.

15 comments:

  1. Nice twister, this should work: replace line 4 with

    public static void SomeMethod(){}

    regards, Jörn

    ReplyDelete
  2. public static void main() throws Throwable

    ReplyDelete
  3. Ho and I forgot :
    public static void SomeMethod() {}

    ReplyDelete
  4. public static void doSomething(){}

    and 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

    ReplyDelete
  5. 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 -

    Replace,
    public void SomeMethod(){};
    with,
    public static void SomeMethod(){};

    Easy!

    ReplyDelete
  6. Ah my earlier solution holds true for the tougher side too! yay!

    ReplyDelete
  7. my solution is based on not assigning the null value to ex.

    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 :)

    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!!
    }
    }

    ReplyDelete
  8. ok.. the "make it tough" does nothing for me :P

    since 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.

    ReplyDelete
  9. ex = null;
    if(ex==null)ex=new Exceptional();
    ex.SomeMethod();

    ReplyDelete
  10. Really dont know rules for your game, but maybe that way?

    1 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 }

    ReplyDelete
  11. BTW, really a nice blog. Gonna read :)

    ReplyDelete
  12. For this thing to work, we just have to make sure that the ex=null; never runs. So, if we do something like

    public 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

    ReplyDelete
  13. public class Exceptional {

    public void SomeMethod(){}

    public static void main(String[] args){
    Exceptional ex = new Exceptional();
    if(ex == null);
    ex.SomeMethod();
    }
    }

    ReplyDelete
  14. One thought is to add "static" to the declaration of SomeMethod(), letting the object reference fall through to the class method.

    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.

    ReplyDelete
  15. public static void SomeMethod(){}

    ReplyDelete

Solution for this question?