This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes NullPointerException when removing objects, for which equals() is overriden, from JComboBox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "NullPointerException when removing objects, for which equals() is overriden, from JComboBox" Watch "NullPointerException when removing objects, for which equals() is overriden, from JComboBox" New topic
Author

NullPointerException when removing objects, for which equals() is overriden, from JComboBox

Sven Andersson
Greenhorn

Joined: Sep 01, 2011
Posts: 2
Hi all,

I have created a small test program that demonstrates the error I am getting when trying to remove previously inserted objects from a JComboBox, for which I override equals()-method:



The program compiles fine, but when running it, I get the NullPointerException, as per stack trace below:



When I remove the overriden equals() method, everything works fine. The java version I am using is 1.6.0_20. Could somebody explain why overriding equals() causes a NullPointerException?

Thank you in advance.

Kind Regards,
Veroslav
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

That's what you get for not doing a null or instanceof check.

luck, db
There are no new questions, but there may be new answers.
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
I believe the NPE is your equals method. Does that method have a prayer of returning a boolean? If so, where does the boolean come from? Sorry to be dense (if I am), but it just doesn't look right.


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Sven Andersson
Greenhorn

Joined: Sep 01, 2011
Posts: 2
Thank you so much for you replies, it is working now! It was a newbish error, sorry for that.

I guess I was expecting it to always compare against the Entry objects, as I was only putting objects of that kind into the JComboBox, but I was wrong.
Perhaps a little explanation of when the object being compared to is not an instance of Entry would be in place?

Thanks again!

Kind Regards,
Veroslav
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
Darryl Burke wrote:That's what you get for not doing a null or instanceof check.

If the isntanceof is true, does one need the (Entry) cast? I'll check it later when I get home. (Note to self.)
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4342
    
    7

Greg Brannon wrote:If the isntanceof is true, does one need the (Entry) cast? I'll check it later when I get home. (Note to self.)

Yes. Without the cast you can't get at the .name member. You need the instanceof check to make sure that the cast is safe, to make sure you never get an InvalidCastException.
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
Matthew Brown wrote:
Greg Brannon wrote:If the isntanceof is true, does one need the (Entry) cast? I'll check it later when I get home. (Note to self.)

Yes. Without the cast you can't get at the .name member. You need the instanceof check to make sure that the cast is safe, to make sure you never get an InvalidCastException.


Understand. Thanks.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Matthew Brown wrote:Yes. Without the cast you can't get at the .name member. You need the instanceof check to make sure that the cast is safe, to make sure you never get an InvalidCastException.


ClassCastException
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

I believe InvalidCastException is from C#.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullPointerException when removing objects, for which equals() is overriden, from JComboBox
 
Similar Threads
XMLEncoder - Stackoverflowerror - overriding equals
Exit this Do-While w/out an int?
Confusion with hashCode() and equals() method
Java Runtime Error
overriding equals() for StringBuilder