File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sven Andersson
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what you get for not doing a null or instanceof check.
 
Greg Brannon
Bartender
Posts: 563
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sven Andersson
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 563
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 563
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe InvalidCastException is from C#.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic