Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

java.lang.ClassCastException: java.lang.String cannot be cast to mycom.lang.MyString

 
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My program has this run time error:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to mycom.lang.MyString
at myproj.client.swing.util.MyStringUtil$MapComparable.compare(MyStringUtil.java:25)


MyString is a third party class which I don't have the source code. It has the toString() method.
MyStringUtil is written by me to process MyString.
MyComboBox should accept both Map<String, String> and Map<String, MyString> as parameters.
How to rewrite my program?
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have two constructors... Let one get Map<String,String> and the other Map<String,MyString>
 
albert kao
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Jai wrote:Have two constructors... Let one get Map<String,String> and the other Map<String,MyString>



It is harder than it looks - because of type erasure of the generic type, the two methods are considered the same:
Duplicate method MyComboBox(Map<String,MyString>) in type MyComboBox client/swing/components MyComboBox.java line 62
Duplicate method MyComboBox(Map<String,String>) in type MyComboBox client/swing/components MyComboBox.java line 69
 
Marshal
Posts: 25838
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

albert kao wrote:It is harder than it looks - because of type erasure of the generic type, the two methods are considered the same



What generic type? There isn't any such thing in your code.
 
albert kao
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

albert kao wrote:It is harder than it looks - because of type erasure of the generic type, the two methods are considered the same



What generic type? There isn't any such thing in your code.



I refer to the java.util.Map generic type.
Adding another constructor with Map as the parameter will result in compile error which I had posted.
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

albert kao wrote:MyString is a third party class which I don't have the source code. It has the toString() method.
...
MyComboBox should accept both Map<String, String> and Map<String, MyString> as parameters.


Why? Does your ComboBox update the MyString?

How to rewrite my program?


Well if the answer to my question above is 'no', I'd just write a method that takes a Map<String, MyString> and converts it to a Map<String, String>; or, if you want to be really clever, write a class that takes a Map<String, MyString> and presents it as a Map<String, String>. Then you don't have to update your MyComboBox at all (or hardly).

Winston
 
Paul Clapham
Marshal
Posts: 25838
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or just work with Map<String, Object>. You don't say what you are doing with this MyString thing; if you're only using its toString() method then using it as an Object would be fine.
 
Winston Gutkowski
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:Or just work with Map<String, Object>.


I guess it would be worth knowing if MyString implements CharSequence. If so, MyComboBox could possibly take a Map<String, ? extends CharSequence> without any other modification required. But it really depends on what it does.

Winston
 
albert kao
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

albert kao wrote:MyString is a third party class which I don't have the source code. It has the toString() method.
...
MyComboBox should accept both Map<String, String> and Map<String, MyString> as parameters
Why? Does your ComboBox update the MyString?

...
Winston



No, ComboBox does not update the MyString.
Now I create MyComboBox2 so that both map1 & map2 are supported:

My goal is to rewrite MyComboBox so that both map1 & map2 are supported because MyComboBox occur in a lot of source codes already.
These are the current sample usage, which will cause casting exception.
Is it possible to rewrite MyComboBox so that the following code will work?


 
Paul Clapham
Marshal
Posts: 25838
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then based on what I've seen there, your best choice is to convert your Map<String, MyString> objects to Map<String, String> objects. This would take about six lines of code, including the line which converts a MyString to a String via the toString() method.
 
albert kao
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[quote=Winston Gutkowski
I guess it would be worth knowing if MyString implements CharSequence. If so, MyComboBox could possibly take a Map<String, ? extends CharSequence> without any other modification required. But it really depends on what it does.

Winston



MyString does not implement CharSequence.
 
Winston Gutkowski
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

albert kao wrote:MyString does not implement CharSequence.


Hmmm. Seems rather odd, since String does. Is it, in fact, a String at all; or just a badly-named class that some "software" company (or Computer Science professor) lobbed over the wall at you? If so, I pity you.

Winston
 
albert kao
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:Then based on what I've seen there, your best choice is to convert your Map<String, MyString> objects to Map<String, String> objects. This would take about six lines of code, including the line which converts a MyString to a String via the toString() method.


Where does the conversion occur?
At the MyComboBox or MyStringUtil or both?
 
Aaaaaand ... we're on the march. Stylin. Get with it tiny ad.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic