aspose file tools*
The moose likes Beginning Java and the fly likes Need help with HashMap object mapping-- type mismatch how to fix? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help with HashMap object mapping-- type mismatch how to fix?" Watch "Need help with HashMap object mapping-- type mismatch how to fix?" New topic
Author

Need help with HashMap object mapping-- type mismatch how to fix?

Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

Ok, my HashMap is set to take variables/objects in this class called TPLVariables. A TPLVariable can be of the type TPLString or TPLInt. I instantiated the HashMap like this:



but then I need it to create the right type of variable, so I put in this:



Trying to use this line in the declaration however gets me
Error: method put in interface Map<K,V> cannot be applied to given types;
variables.put(name, stringDeclaration);
required: String, StringSplit.TPLVariables
found: String, StringSplit.TPLString



So what do I do now? I'm trying to see how I can have both types use the same method. Thanks!


"How do you define fool?"
"I don't attempt it. I wait for demonstrations. They inevitably surpass my imagination."
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18993
    
    8

Is the TPLString class a subclass of TPLVariable, or is it a subclass of TPLVariables?
Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

TPLString and TPLInt are subclasses of the TPLVariable class-- oh wait, did I just make a mistake in typing the superclass? TPLVariable is an abstract class though.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18993
    
    8

Doesn't matter if TPLVariable is abstract. It does matter if TPLVariables is another class, of which TPLString is not a subclass.
Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

TPLVariables is the class that has my HashMap in it, and TPLVariable is the abstract superclass for TPLString and TPLInt, so it's definitely not a subclass of TPLVariables.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18993
    
    8

Okay. Then you would want this:

Wouldn't you? After all the values of the Map are going to be TPLVariable objects, not TPLVariables objects.

(It appears that by choosing those two almost-identical names for those classes you have caused yourself some confusion.)
Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

Uhm yeah, thanks for posting the fix, though I came to that conclusion myself already. Note to self-- make sure all names are very different in future. I'm getting another error message now though: This line is in a different class implementing an interface called Directive (different HashMap), but needs to get the variable "name" from TPLVariable... I'm getting a "cannot find symbol" error for "name", and it's also throwing this "non-static variable variables cannot be referenced from a static context".

The class in question:

>
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Noam Ingalls wrote:I'm getting a "cannot find symbol" error for "name", and it's also throwing this "non-static variable variables cannot be referenced from a static context".


This looks like the same thing as in the new thread you started. If so, please indicate it here so that people don't start answering the same question in two different places.


[Fixed Quote]
Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

Should I ask to close the other thread then or close this one? I think closing this one would be better since the original issue is kinda resolved.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3099
    
  33

Noam Ingalls wrote:Should I ask to close the other thread then or close this one? I think closing this one would be better since the original issue is kinda resolved.


Is this the other thread?


Mohamed Sanaulla | My Blog
Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

Mohamed Sanaulla wrote:
Noam Ingalls wrote:Should I ask to close the other thread then or close this one? I think closing this one would be better since the original issue is kinda resolved.


Is this the other thread?


No, but come down to it the problem I was having when I asked that is fairly similar to my current issue here-- in fact, ugh, apologies, I think I've been posting too many new threads perhaps-- the problems are related, being all one project.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help with HashMap object mapping-- type mismatch how to fix?