1

I have a problem with invoking String.valueOf method. As argument I passed generic method which returns Integer type. And then exception is thrown because program try to cast that returned Integer to char[] to invoke String.valueOf method. As I know String has overloaded valueOf() method and one of them can take Integer parameter.

Below is screenshot from Eclipse expression evaluation which shows situation.

Evaluated expression

This line throws ClassCastException

String quality = String.valueOf(model.get(property));

This is what model.get(property) invokes:

public <X> X get(String property) {
        X value = null;
        try {
            if (allowNestedValues && NestedModelUtil.isNestedProperty(property)) {
                return (X) NestedModelUtil.getNestedValue(this, property);
            }
            if (map != null) {
                Object objValue = map.get(property);
                if (objValue != null)
                    value = (X) objValue;
            }
        } catch (Exception e) {
        }
        return map == null ? null : value;
    }

EDIT:

I added this condition and compiler goes inside the if statement

if(model.get(property) instanceof Integer)
{
    quality = String.valueOf(model.get(property));
}

enter image description here

10
  • 2
    Stop describing your code, show your code. And post the stacktrace. Commented Jan 21, 2019 at 14:59
  • Can you edit your question and add a minimal reproducible example of code which reproduces this exception? Commented Jan 21, 2019 at 15:00
  • How do you expect the Java compiler to correctly infer which String.valueOf variant to use? Commented Jan 21, 2019 at 15:03
  • @MarkRotteveel then how Java compiler know that it is Integer instance ? Commented Jan 21, 2019 at 15:04
  • 2
    valueOf has overloads for Object, char[] and various primitive types. char[] is the "most specific" one and is therefore chosen (presumably). Commented Jan 21, 2019 at 15:18

2 Answers 2

2

How is the compiler supposed to infer the correct X the way you call it?

Split it into

YourType qualityRaw = model.get(property);
String quality = String.valueOf(qualityRaw);
Sign up to request clarification or add additional context in comments.

5 Comments

Doing String.valueOf(model.<YourType>get(property) might also work.
Why instanceof Integer returns true ?
Check attached screenshot where instanceof is evaluated and value is true
@RafałSokalski You are confusing runtime types with compile time inferred types. Method resolution is done at compile time, and here that inferrence turns out to be wrong which results in a runtime class cast exception.
@luk2302 Thank you
1

You should try Integer.toString method if you know that your return type is of Integer type.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.