2

What is wrong with below code ?

List<Object[]> currencies = null;
List<String> currencyList = new ArrayList<String>();

//code to fetch currency
String currencySql = "select C.Currency from Currency C";
Query currencyQuery = this.em.createQuery(currencySql);

currencies = currencyQuery.getResultList();

for (Object[] currency : currencies) {    //getting runtime error here
   for (int i = 0; i < currency.length; i++) {
            currencyList.add(currency[i].toString());
        }
    }

Getting runtime error in first line of for loop as: java.lang.ClassCastException: java.lang.String incompatible with [Ljava.lang.Object;

The code compiles alright.

What is the issue ?

4 Answers 4

2

I'm not sure why you are thinking that the Query would return a list of Object arrays.

The query will most likely return a list of Strings (unless the Currency entity contains a Currency object, in which case I'd revisit the data design), so operating under the assumption that this is a scalar query, your code can be simplified to:

String currencySql = "select C.Currency from Currency C";
Query currencyQuery = this.em.createQuery(currencySql);

List<String> currencyList = (List<String>) currencyQuery.getResultList();
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks!! So if the query is for several columns, in that case getResultList will return list of Object arrays. But in this case it was just one column, which was String, it returned a list of Strings. Correct ???
It depends on the concrete implementation of your EntityManager, but it certainly looks that way.
0

Check in the debugger what is in currencies variable after

currencyQuery.getResultList();

Probably it is not a list of Object[], but a list of strings.

2 Comments

changing currencies to List<String[]> threw java.lang.ClassCastException: java.lang.String incompatible with [Ljava.lang.String;
I mean not List<String[]>, but List<String>, because looks like your query returns one column, that is converted to list of strings.
0

The problem is that currencyList should be of type List and not List<Object[]>. This also means that the currency variable in your loop should be of type Object, so that it is actually compatible with String (that's where the exception is coming from). You'll need to then verify that you've got a string in it.

Alternatively, you've got enough information there in the SQL to know that the result will really be a List<String>, which will in turn allow you to declare currency to be of type String. But that will involve a formally-unsafe cast; this will be a place where you know it is correct but the compiler doesn't, so you'll get a warning. It would be an appropriate place to suppress the warning (just on that one assignment/cast though; you want to try to avoid suppressing warnings because that can conceal serious problems).

3 Comments

And the result is definitely not a list of arrays of anything.
So is it that hibernate decides from the SQL whether it will return list of strings or list of objects based on the type of things returned ???
It knows the types. It knows how many values there are in each row. It says “one value per row; row value is that value” rather than wastefully putting it inside an array of length 1.
0

I think there is something wrong in that you are getting a list of currencies from the query. You dont need to create another array. Try this

List<String> currencyList = new ArrayList<String>();

//code to fetch currency
String currencySql = "select C.Currency from Currency C";
Query currencyQuery = this.em.createQuery(currencySql);

currencies = currencyQuery.getResultList();

for (String currency : currencies) {    
    currencyList.add(currency);
}

1 Comment

i am not sure what the getResultList returns. I would assume its a List. Or does it return Object Array?

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.