comparing an object with null

From: Asad Khan (uoft_cstutor_at_yahoo.com)
Date: 10/31/03


Date: Fri, 31 Oct 2003 16:20:47 GMT

I have the following method in a class 'foo'.

  public boolean equals(Object o) {
    if (!(o.equals(null))) {
       // do some stuff
    }
  }

  The problem I am having is that, if the object that is passed in is
instanceof foo, then it recursively calls the equals method, and I get a
nullpointer exception. So how can I compare if the object that is being
passed in is null or not?

Thanks.



Relevant Pages

  • Confusion with templates, generics, and instanceof
    ... public FooWrapper(T foo) { ... equals method. ... public boolean equals{ ... Basically this doesn't work because "o instanceof T" (where T is a ...
    (comp.lang.java.programmer)
  • Re: OOP - a question about database access
    ... > public boolean performFooAction ... > public class MyFooAction ... > public boolean perform(Foo foo) ... > Then the client code can operate on all objects in a query as follows: ...
    (comp.object)
  • Re: comparing an object with null
    ... > I have the following method in a class 'foo'. ... > public boolean equals{ ... sole pointer in Java! ...
    (comp.lang.java)
  • Re: Newbie question: How to write a set?
    ... ensure only unique instances of Foo are placed in the set? ... public boolean equals{ ... public int hashCode() { ... // equals() takes into consideration ...
    (comp.lang.java.programmer)
  • Re: OOP - a question about database access
    ... public boolean performFooAction ... public class MyFooAction ... public boolean perform(Foo foo) ... IFooAction myFooAction = new MyFooAction; ...
    (comp.object)