java.lang.String 不能转换为 [Ljava.lang.String;

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39638394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:19:38  来源:igfitidea点击:

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

javajpaclasscastexception

提问by Giorgos

I have the following and I get the error

我有以下内容,但出现错误

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

java.lang.String 不能转换为 [Ljava.lang.String;

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

我已更改为Object[]String[]因为我遇到了下一个错误:

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

java.lang.Object 不能转换为 [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;
}

采纳答案by Nicolas Filotto

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

java.lang.String 不能转换为 [Ljava.lang.String;

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

当您尝试将 aString强制转换为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.

对我来说,您收到此错误是因为query.getResultList()返回 a List<String>orList<Object>而不是List<String[]>这样,当您尝试将值转换为 a 时,String[]您会收到此异常。



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

根据 Javadoc createNativeQuery(String) 返回类型结果Object[]或类型结果,Object如果选择列表中只有一列。

Approach #1

方法#1

One simple way to fix it, could be to rely on the raw typefor 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

方法#2

A more elegant way could be to create a POJOand use createNativeQuery(String sqlString, Class resultClass)to create your query, this way it will automatically map your columns with the fields of your POJO

一种更优雅的方法可能是创建一个POJO并用于createNativeQuery(String sqlString, Class resultClass)创建您的查询,这样它会自动将您的列与您的字段映射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();
    ...
}

回答by OldCurmudgeon

The line

线

ArrayList a = new ArrayList();

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

原始类型,因此(可能)代码中的所有对象都可以作为Object.

Try

尝试

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

回答by DaImmi

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

在您的代码中的某个时刻,您试图将 String 转换为 String[]。您的堆栈跟踪将告诉您确切位置。

Apart from that your code has plenty of other issues.

除此之外,您的代码还有很多其他问题。

回答by Nimesh

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