Join the Stack Overflow Community
Stack Overflow is a community of 6.3 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have the following and I take the error java.lang.String cannot be cast to [Ljava.lang.String;

I have change the Object[] to String[] because before that I took 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);
//        Query query = getEntityManager().createNativeQuery(SelectStatement, ".findAll");

        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;

    }
share|improve this question
    
What is your selectstatement? can you write query? – Naman Sep 22 at 11:57
    
Naman SELECT PASSWD FROM AUDITORS WHERE USERNAME = '1114' But as you userstand the query is parameter – Giorgos Sep 22 at 11:58
1  
Why would that return a List<String[]> ? It looks like it's returning a List<String>, hence the error. – Jorn Vernee Sep 22 at 12:01
    
yes it will come List<String> because in query you have only one column PASSWD – Naman Sep 22 at 12:01
    
Because the query is parameter (SelectStatement) is posssible to return List(String[]) – Giorgos Sep 22 at 12:03
up vote 0 down vote accepted

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 a POJO and use createNativeQuery(String sqlString, Class resultClass) to create your query, this way it will automatically map your columns with the fields of your POJO

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();
    ...
}
share|improve this answer
    
Nicolas Filotto you have right, but the Select query is parameter so on some time return List<String> if the SELECT have one column and List<String[][]> if have more than one. Any Idea how to fix that? – Giorgos Sep 22 at 12:33
    
I guess you mean List<String[]> if you have more than one column – Nicolas Filotto Sep 22 at 12:36
    
yes I mean List<String[]> sorry. – Giorgos Sep 22 at 12:40
    
If my answer is good enough for you don't hesitate to up vote too, thx in advance – Nicolas Filotto Sep 22 at 14:00
    
@NicolasFilotto, the second statement you make This error occurs ... is not strictly correct. I think you mean something like: "This error occurs when you try to cast a String object to an array of String objects String[]". If you use the word List as you have then you implicitly mean java.util.List – Steve C Sep 22 at 23:19

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<>();
share|improve this answer

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.

share|improve this answer

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;
}
share|improve this answer
    
Please see my comment at Nicolas Filotto you have right, but the Select query is parameter so on some time return List<String> if the SELECT have one column and List<String[]> if have more than one. – Giorgos Sep 22 at 12:43
    
you are right @nicolas but here he has mentioned his query. I have already asked him that what is the query and it contains only one select column only. – Naman Sep 22 at 12:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.