首页 > 解决方案 > java.lang.ClassCastException:[Ljava.lang.Object;不能转换为 java.util.List

问题描述

我有一个List<List<String>> dataTableList,我想从那里获取一个特定的列表并将其放在我的列表中,List<String> dataList以便我可以遍历该特定列表的值并对其进行更改。

但是,每当我尝试这样做时,我总是会收到以下错误:

java.lang.ClassCastException:[Ljava.lang.Object;不能转换为 java.util.List。

这是我如何尝试将特定列表从 dataTableList 分配给 dataList 的示例:

//First I looped through the List of Lists and called each list fetched as dataList
for(List<String> dataList : getTryLang().getDataTableList()){ 
    //then, I created an iterator to be used later when I return the List I fetched with altered value
    int iter = 0;
    //next, I created a for-loop to iterate through the values of the List the I feched
    for(int x; x < dataList.size(); x++){
        //here, I formatted each value to amount or currency format.
        dataList.set(x, getDataConvert().convertAmount(dataList.get(x)));
        //finally, after I formatted everything, I returned it or set it to the specific index it's been located before
        getTryLang().getDataTableList().set(iter, dataList);
    }
    iter++;
}

编辑:

这是我的代码,我修改了其中的一些但没有包含一些,以便我可以专注于表达问题发生的位置。

这是我的 TryLang.java:

@ManagedBean
@SessionScoped
public class TryLang implements Serializable {

   public TryLang() {}

   //declare
   private List<List<String>> dataTableList; 


    //getter setter
    public List<List<String>> getDataTableList() {
        return dataTableList == null ? dataTableList = new ArrayList<>() : dataTableList;
    }

    public void setDataTableList(List<List<String>> dataTableList) {
        this.dataTableList = dataTableList;
    }
}

然后这是我的 BookOfAccountsController.java:

@ManagedBean
@RequestScoped
public class BooksOfAccountsController implements Serializable {


    public BooksOfAccountsController() {}

    //declare
    @ManagedProperty(value = "#{dataConvert}")
    private DataConvert dataConvert;
    @ManagedProperty(value = "#{tryLang}")
    private TryLang tryLang;

    //getter setter NOTE: I wouldn't include other getter setters to shorten the code here :)
    public TryLang getTryLang() {
         return tryLang == null ? tryLang = new TryLang() : tryLang;
    }

    public void setTryLang(TryLang tryLang) {
         this.tryLang = tryLang;
    }

    //I would just go straight to the method instead
public void runBooksOfAccounts() throws SystemException, SQLException {
   //So there are dbCons here to connect on my DB and all. And I'll just go straight on where the List<List<String>> is being set

  //Here's where the List<List<String>> is being set
    getTryLang().setDataTableList(getCemf().getFdemf().createEntityManager().createNativeQuery("SELECT crj.* FROM crj_rep crj").getResultList());

    getTryLang().setDataTableColumns(getCemf().getFdemf().createEntityManager().createNativeQuery("SELECT col.column_name FROM information_schema.columns col WHERE table_schema = 'public' AND table_name = 'crj_rep'").getResultList());

    for (int x = 0; x < getTryLang().getDataTableColumns().size(); x++) {
        try {

            Integer.parseInt(getTryLang().getDataTableColumns().get(x));

            getTryLang().getDataTableColumns().set(x, getDataConvert().accountCodeConvert(getTryLang().getDataTableColumns().get(x)));
            //then here is where the error points at
            for (List<String> dataList : getTryLang().getDataTableList()) {
                try{
                    int iter = 0;
                    dataList.set(x, getDataConvert().convertAmount(new BigDecimal(dataList.get(x))));
                    getTryLang().getDataTableList().set(iter, dataList);
                    iter++;
                }catch(ClassCastException ne){
                    System.out.println("cannot convert " + ne);
                }
            }
         } catch (NumberFormatException ne) {
             //print the error
         }
      }
   }
}

标签: classcastexceptionjava-ee-7

解决方案


推荐阅读