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

We Are Going To Discuss About java.lang.String cannot be cast to [Ljava.lang.String;. So lets Start this Java Article.

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

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

    At some point in your code you're trying to cast a String to String[]. Your stack trace will tell you where exactly.
    Apart from that your code has plenty of other issues.

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

    At some point in your code you're trying to cast a String to String[]. Your stack trace will tell you where exactly.
    Apart from that your code has plenty of other issues.

Solution 1

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();
    ...
}
Advertisements

Original Author Nicolas Filotto Of This Content

Solution 2

The line

ArrayList a = new ArrayList();

is a raw type and so (potentailly) all objects in the code could be treaded as Object.

Try

ArrayList<String[][]> a = new ArrayList<>();
Advertisements

Original Author OldCurmudgeon Of This Content

Solution 3

At some point in your code you’re trying to cast a String to String[]. Your stack trace will tell you where exactly.

Apart from that your code has plenty of other issues.

Advertisements

Original Author DaImmi Of This Content

Solution 4

Do like this:

List<String> resultList = (List<String>) query.getResultList();
if (!resultList.equals(Collections.emptyList())) {
    rowcount = resultList.size();
}

if (rowcount > 0) {
     colcount = resultList.get(0).length;
}
Advertisements

Original Author Nimesh Of This Content

Conclusion

Advertisements

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment