1

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

Sandun Chathuranga
  • 2,242
  • 2
  • 13
  • 27
jros
  • 714
  • 1
  • 10
  • 33
  • 3
    Show us the relevant code. How is caseInsensitiveMap declared, initialized and populated? – JB Nizet May 03 '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: http://stackoverflow.com/questions/3442090/java-what-is-this-ljava-lang-object – Florian Schaetz May 03 '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 03 '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 03 '16 at 19:03
  • @MadPhysicist doing String[] s = ... gives me compilation errors – jros May 03 '16 at 19:05
  • 1
    Do you get any warning at the line `Map reqMap = reqProperties.getMap();`? – Eran May 03 '16 at 19:06
  • @Eran no warnings there – jros May 03 '16 at 19:06
  • What does `getMap()` method of `TypedProperty` return? – Eran May 03 '16 at 19:08
  • You using this [TypedProperty](https://www.ibm.com/support/knowledgecenter/SSZLC2_7.0.0/com.ibm.commerce.api.doc/com/ibm/commerce/datatype/TypedProperty.html)? – OneCricketeer May 03 '16 at 19:08
  • @Eran Map com.ibm.commerce.datatype.TypedProperty.getMap() – jros May 03 '16 at 19:09
  • Well, based on the [TypedProperty Javadoc](https://www.ibm.com/support/knowledgecenter/SSZLC2_7.0.0/com.ibm.commerce.api.doc/com/ibm/commerce/datatype/TypedProperty.html), the `Map` returned by `getMap` may contain non-String values (including String arrays), so assigning it to a `Map` is unsafe, and allows the exception you got to occur. – Eran May 03 '16 at 19:17

1 Answers1

2

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);
Judge Mental
  • 5,209
  • 17
  • 22
  • You were correct. Whoever wrote the code that set `caseInsensitiveMap` (as well as myself) did not realize that TypedProperties in no way guarantee . – jros May 03 '16 at 19:33