comparing Object with null

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Asad Khan

    comparing Object with null

    public boolean equals(Object o) {
    if (!(o.equals(nul l))) { // *
    if (o instanceof foo) {
    if (this.equals(o) ) {
    return true;
    }
    }
    }
    return false;
    }

    I have the above method in a class foo. When I run the following code:

    f.equals(f)

    where f is an object of class foo, then I get a nullpointerexce ption in this
    method. The reason is that since f is of type foo, in line * it recursively
    calls the equals method and then o becomes null, and of course I can't
    dereference null.

    So how can I check if an object is null or not?

    Thanks.


  • Bryce (Work)

    #2
    Re: comparing Object with null

    On Fri, 31 Oct 2003 16:20:48 GMT, "Asad Khan" <uoft_cstutor@y ahoo.com>
    wrote:
    [color=blue]
    > public boolean equals(Object o) {
    > if (!(o.equals(nul l))) { // *
    > if (o instanceof foo) {
    > if (this.equals(o) ) {
    > return true;
    > }
    > }
    > }
    > return false;
    > }
    >
    >I have the above method in a class foo. When I run the following code:
    >
    >f.equals(f)
    >
    >where f is an object of class foo, then I get a nullpointerexce ption in this
    >method. The reason is that since f is of type foo, in line * it recursively
    >calls the equals method and then o becomes null, and of course I can't
    >dereference null.
    >
    >So how can I check if an object is null or not?[/color]

    Did you try if (o != null)

    Comment

    • noel wood

      #3
      Re: comparing Object with null

      Further to this answer different objects implement the method equals
      differently. For example String objects implement it as a stright
      comparrison of the contents of the string. So the equals method does not
      tell you if one object is the same as another object. For this you need to
      use the = = operator(withou t the space).
      "Bryce (Work)" <spamtrap@berze rker-soft.com> wrote in message
      news:rok5qvo2ib v30antmat9ao6s3 [email protected] om...[color=blue]
      > On Fri, 31 Oct 2003 16:20:48 GMT, "Asad Khan" <uoft_cstutor@y ahoo.com>
      > wrote:
      >[color=green]
      > > public boolean equals(Object o) {
      > > if (!(o.equals(nul l))) { // *
      > > if (o instanceof foo) {
      > > if (this.equals(o) ) {
      > > return true;
      > > }
      > > }
      > > }
      > > return false;
      > > }
      > >
      > >I have the above method in a class foo. When I run the following code:
      > >
      > >f.equals(f)
      > >
      > >where f is an object of class foo, then I get a nullpointerexce ption in[/color][/color]
      this[color=blue][color=green]
      > >method. The reason is that since f is of type foo, in line * it[/color][/color]
      recursively[color=blue][color=green]
      > >calls the equals method and then o becomes null, and of course I can't
      > >dereference null.
      > >
      > >So how can I check if an object is null or not?[/color]
      >
      > Did you try if (o != null)[/color]



      Comment

      • Bryan E. Boone

        #4
        Re: comparing Object with null

        You can collapse it to:
        public boolean equals(Object o) {
        if(!(o instanceof foo)) {return false;} //this catches the null case
        too
        foo f = (foo)o; //f will _never_ be null here
        // else check for whatever attributes makes foos equal
        // if you do this.equals(f) like in your original example it will be an
        infinite loop
        // and [probably] have a StackOverFlow exception
        }
        "Bryce (Work)" <spamtrap@berze rker-soft.com> wrote in message
        news:rok5qvo2ib v30antmat9ao6s3 [email protected] om...[color=blue]
        > On Fri, 31 Oct 2003 16:20:48 GMT, "Asad Khan" <uoft_cstutor@y ahoo.com>
        > wrote:
        >[color=green]
        > > public boolean equals(Object o) {
        > > if (!(o.equals(nul l))) { // *
        > > if (o instanceof foo) {
        > > if (this.equals(o) ) {
        > > return true;
        > > }
        > > }
        > > }
        > > return false;
        > > }
        > >
        > >I have the above method in a class foo. When I run the following code:
        > >
        > >f.equals(f)
        > >
        > >where f is an object of class foo, then I get a nullpointerexce ption in[/color][/color]
        this[color=blue][color=green]
        > >method. The reason is that since f is of type foo, in line * it[/color][/color]
        recursively[color=blue][color=green]
        > >calls the equals method and then o becomes null, and of course I can't
        > >dereference null.
        > >
        > >So how can I check if an object is null or not?[/color]
        >
        > Did you try if (o != null)[/color]


        Comment

        Working...