weikipeng / javacpp

Automatically exported from code.google.com/p/javacpp
GNU General Public License v2.0
0 stars 0 forks source link

Pointer.equals fail when obj is null #22

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. class X extends Pointer
2. X ptr = new X();
3. X.equals(null);

What is the expected output? What do you see instead?
Should show false, but is throwing a NullPointerException

Fix would be in Pointer.java:

    @Override public boolean equals(Object obj) {
        if (obj == null && isNull()) {
            return true;
//fix <--
        } else if (obj == null || isNull()) {
               return false;
//-->
        } else if (obj.getClass() != getClass()) {
            return false;
        } else {
            Pointer other = (Pointer)obj;
            return address == other.address && position == other.position;
        }
    }

Original issue reported on code.google.com by bid...@gmail.com on 31 Jul 2012 at 6:59

GoogleCodeExporter commented 9 years ago
Yup, that makes sense, will fix that :) thanks!

Original comment by samuel.a...@gmail.com on 1 Aug 2012 at 1:03

GoogleCodeExporter commented 9 years ago
You are welcome.

Original comment by bid...@gmail.com on 1 Aug 2012 at 11:27

GoogleCodeExporter commented 9 years ago
Actually this looks more correct IMO:
    if ((obj == null || (obj.isNull() && obj.getClass() == getClass()) && isNull()) {
        return true;
    } ...
What do you think?

Original comment by samuel.a...@gmail.com on 1 Aug 2012 at 12:06

GoogleCodeExporter commented 9 years ago
Whoops, and this:
    } else if (obj == null || obj.getClass() != getClass()) {
        return false;
    } ...

Hum, doesn't look entirely right though. I'll have to think a bit more about 
that

Original comment by samuel.a...@gmail.com on 1 Aug 2012 at 12:09

GoogleCodeExporter commented 9 years ago
Ak, now this looks right:
    if (obj == null) {
        return isNull();
    } else if (obj.getClass() != getClass()) {
        return false;
    } else ...
Let me know if you have any issues with that! thanks

Original comment by samuel.a...@gmail.com on 1 Aug 2012 at 12:13

GoogleCodeExporter commented 9 years ago
That should work. When obj is null, it was crashing on Android trying to access 
getClass(). I forgot to mention that earlier. 

Original comment by bid...@gmail.com on 1 Aug 2012 at 12:39

GoogleCodeExporter commented 9 years ago
It's in JavaCPP 0.3! Let me know if there are still some issues.

Original comment by samuel.a...@gmail.com on 5 Nov 2012 at 11:16