‘this’ can’t be null

dead_fish1.jpg

 

This is the first entry on a category I’d like to name “Code bloopers”. I recently got a message about a piece of code that was throwing a null pointer, it was basically of this form:

synchronized(this.foobar)

This is an easy one. If you think it’s because thisis null, you have to go back to programming 101 and don’t collect $200 if you pass Go. I don’t want to sound mean, but yes, this is one of those bloopers that if you get it wrong, I hope it’s because you are half asleep or on drugs.

Remember kids, this can’t be null. In case you have been a victim of this code blooper, don’t feel bad, you are not the only one confused by this. It’s the only case (that I could find in the internet) of somebody else thinking this can be null, and I hope that gives you some comfort.

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

1 Comment »

 
  • this_can_be_null says:

    class myclass1

    {

    public:

    void setint( int _i )

    {

    if ( this != NULL )

    {

    if ( this->i != 17 )

    {

    i = _i;

    }

    }

    };

    private:

    int i;

    };

    int main()

    {

    myclass1* pmyobject1; // = new myclass1;

    pmyobject1 = NULL;

    pmyobject1->setint( 17 );

    delete pmyobject1;

    return 0;

    }

 

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

WP SlimStat