Vicky
Vicky

Reputation: 17375

class cast exception: java.lang.String incompatible with [Ljava.lang.Object;

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 ?

Upvotes: 2

Views: 38267

Answers (4)

beny23
beny23

Reputation: 35018

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

Upvotes: 2

Sid Malani
Sid Malani

Reputation: 2116

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);
}

Upvotes: 0

Donal Fellows
Donal Fellows

Reputation: 137587

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

Upvotes: 0

dbf
dbf

Reputation: 6499

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.

Upvotes: 0

Related Questions