I'm doing the following

 String s = caseInsensitiveMap.get("buyerCode");

and I'm getting the error

java.lang.ClassCastException: [Ljava.lang.String; incompatible with java.lang.String

I just cannot figure out what I'm doing wrong. Googling the answer seems to point towards needing to use a String[] somewhere, but I have no idea where.

More relevent information:

caseInsensitiveMap : Map caseInsensitiveMap - com.msw.commerce.me.commands.MSWOrgCmdImpl.setRequestProperties(TypedProperty)

.get() : String java.util.Map.get(Object key)

I have also attempted to do

String s = caseInsensitiveMap.get((Object) "buyerCode");

to explicitly cast the string "buyerCode" to it's needed Object type, but I get the same error.

Can someone please tell me what I'm doing wrong? From what I can see, I'm matching all of the types here. .get() takes an Object, and I'm giving it an object. It returns a String, and I'm assigning it to a String.


Edit: more code

public void setRequestProperties(TypedProperty reqProperties)
        throws ECException {
    Map<String, String> reqMap = reqProperties.getMap();
    Map<String, String> caseInsensitiveMap = new TreeMap<String, String>(
            String.CASE_INSENSITIVE_ORDER);
    caseInsensitiveMap.putAll(reqMap);

Here are the docs for TypedProperty

share|improve this question
3  
Show us the relevant code. How is caseInsensitiveMap declared, initialized and populated? – JB Nizet May 3 '16 at 19:00
2  
[Ljava.lang.String means String[], in other words, you are getting an array of Strings, which is not the same as a single String. See also here: stackoverflow.com/questions/3442090/… – Florian Schaetz May 3 '16 at 19:01
    
Your map contains a String[] as the value instead of the String you are expecting. Maps can generally contain any type of object and in your case, you are inserting the wrong type without realizing it. – Mad Physicist May 3 '16 at 19:02
    
Based on the error message, caseInsensitiveMap.get() is returning an array of Strings, not a String. Without your code, it's impossible to analyze further. – Mike Harris May 3 '16 at 19:03
1  
Do you get any warning at the line Map<String, String> reqMap = reqProperties.getMap();? – Eran May 3 '16 at 19:06
up vote 2 down vote accepted

It's almost a certainty that TypedProperty.getMap() is heterogeneous. It's really a map from String keys to Objects, and it's therefore unsafe to assign it to a variable of type Map< String, String >. If you aren't getting a warning at the line

Map<String, String> reqMap = reqProperties.getMap();

it's probably because the warnings about unchecked conversions are disabled in your development environment.

The problem is not the type of the key; the problem is the type of the value you're trying to add to your TreeMap. You can't cast an array of String to a String, so putAll() is going to fail.

Try declaring your TreeMap as

Map<String, Object> reqMap = reqProperties.getMap();
Map<String, Object> caseInsensitiveMap = new TreeMap<String, Object>(
        String.CASE_INSENSITIVE_ORDER);
share|improve this answer
    
You were correct. Whoever wrote the code that set caseInsensitiveMap (as well as myself) did not realize that TypedProperties in no way guarantee <String, String>. – jros May 3 '16 at 19:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.