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 ?
I’m not sure why you are thinking that the Query would return a list of
Objectarrays.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: