jros jros - 8 months ago 44
Java Question

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

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
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
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.
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>(

Here are the docs for TypedProperty


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>(