Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.String cannot be cast to [Ljava.lang.String;

I have the following and I get the error

java.lang.String cannot be cast to [Ljava.lang.String;

I have changed the Object[] to String[] because I faced the next error:

java.lang.Object cannot be cast to [Ljava.lang.String;

Any idea?

private Collection queryStatement(String SelectStatement) {

    int colcount = 0;
    int rowcount = 0;
    int rowcounter = 0;

    ArrayList a = new ArrayList();

    Query query = getEntityManager().createNativeQuery(SelectStatement);

    List<String[]> resultList = (List<String[]>) query.getResultList();

    if (!resultList.equals(Collections.emptyList())) {
        rowcount = resultList.size();
    }

    if (rowcount > 0) {
        colcount = ((String[]) query.getResultList().get(0)).length;
    }

    rows = rowcount;
    cols = colcount;

    String[][] array = new String[rowcount][colcount];

    for (String[] obj : resultList) {
        String[] record = new String[colcount];
        for (int colCounter = 0; colCounter < colcount; colCounter++) {
            record[colCounter] = safeValue(obj[colCounter]+"");
        }

        array[ rowcounter++] = (String[]) record;
    }
    a.add(array);
    return a;
}
like image 552
Giorgos Avatar asked Sep 22 '16 11:09

Giorgos


People also ask

How do I resolve Java Lang ClassCastException error?

// type cast an parent type to its child type. In order to deal with ClassCastException be careful that when you're trying to typecast an object of a class into another class ensure that the new type belongs to one of its parent classes or do not try to typecast a parent object to its child type.

What is Ljava Lang String?

Ljava.lang.String stands for the string class (L followed by class/interface name) Few Examples: Class. forName("[D") -> Array of primitive double. Class.

How to avoid class cast exception in Java?

To prevent the ClassCastException exception, one should be careful when casting objects to a specific class or interface and ensure that the target type is a child of the source type, and that the actual object is an instance of that type.


1 Answers

java.lang.String cannot be cast to [Ljava.lang.String;

This error occurs when you try to cast a String to an array of String.

For example:

List list = new ArrayList<>();
list.add("foo");
String[] values = (String[])list.get(0); -> throws the exception

For me you get this error because query.getResultList() returns a List<String> or List<Object> instead of List<String[]> such that when you try to cast a value as a String[] you get this exception.


According to the Javadoc createNativeQuery(String) returns a result of type Object[] or a result of type Object if there is only one column in the select list.

Approach #1

One simple way to fix it, could be to rely on the raw type for the result (it is not the most elegant approach but the simplest one) then later you can check the type of the content of the list to cast it properly.

List result = query.getResultList();

Then to check the type you can proceed as next:

if (resultList.isEmpty() || resultList.get(0) instanceof Object[]) {
    // Several columns in the result
    List<Object[]> resultList = (List<Object[]>) result;
    // The rest of your current code here
} else {
    // Only one column in the result
    List<Object> resultList = (List<Object>) result;
    ...
}

Approach #2

A more elegant way could be to create an Entity class and use createNativeQuery(String sqlString, Class entityClass) to create your query, this way it will automatically map your columns with the fields of your Entity

Here is how it could look like

private Collection<T> queryStatement(String SelectStatement, Class<T> resultType) {
    ...
    Query query = getEntityManager().createNativeQuery(SelectStatement, resultType);
    List<T> resultList = (List<T>) query.getResultList();
    ...
}
like image 152
Nicolas Filotto Avatar answered Oct 05 '22 10:10

Nicolas Filotto