comparing an object with null

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

    comparing an object with null

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

    public boolean equals(Object o) {
    if (!(o.equals(nul l))) {
    // 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.


  • Glen Herrmannsfeldt

    #2
    Re: comparing an object with null


    "Asad Khan" <uoft_cstutor@y ahoo.com> wrote in message
    news:zZvob.1311 73$h61.37249@ne ws01.bloor.is.n et.cable.rogers .com...[color=blue]
    > I have the following method in a class 'foo'.
    >
    > public boolean equals(Object o) {
    > if (!(o.equals(nul l))) {
    > // 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?[/color]

    Remember, objects are not passed, object references are passed.

    -- glen


    Comment

    • hiwa

      #3
      Re: comparing an object with null

      "Asad Khan" <uoft_cstutor@y ahoo.com> wrote in message news:<zZvob.131 173$h61.37249@n ews01.bloor.is. net.cable.roger s.com>...[color=blue]
      > I have the following method in a class 'foo'.
      >
      > public boolean equals(Object o) {
      > if (!(o.equals(nul l))) {
      > // 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.[/color]
      public boolean equals(Object o) {
      if (!(o == null)){ //null is not an Object, that is 'none-object'
      place holder refrence ... sole pointer in Java!
      // do some stuff
      }
      else[
      return false;
      }
      }

      Comment

      Working...