• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's what you get for not doing a null or instanceof check.
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe InvalidCastException is from C#.
 
What do you have to say for yourself? Hmmm? Anything? And you call yourself a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic