Local variable wtf



  • Found this in some Java code:

    class Base {
      protected Object reference = null;
    

    public void processStuff() {
    reference.someMethod(); // throws null pointer exception here 'cuz only
    // the 'reference' in the sub class is set
    }
    }

    class Sub extends Base {
    protected Object reference = null;
    public void setReference(Object ref) {
    reference = ref;
    }
    }

    <SNIP this of layers more several>

    Then some method, called on the most sub-classed object, and which is implemented in only the class Base was called:

    class SubSub extends Sub {
    protected Object reference = null;
    public void setReference(Object ref) {
    reference = ref;
    }

    public void callback() {
    processStuff();
    }
    }



  • The above was anonymized to protect my job

     



  • @tookie tookie said:

    The above was anonymized to protect my job

     

    Ah, so YOU wrote the above code...



  • @impslayer said:

    @tookie tookie said:

    The above was anonymized to protect my job

     

    Ah, so YOU wrote the above code...

    Only the post, not the code ;)



  • Welcome to the Strategy design pattern. Thanks for playing, please try again.


Log in to reply