java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.util.List

本秂侑毒 提交于 2020-01-16 10:02:51

问题


I have a List<List<String>> dataTableList and I would like to get a specific list from there and put it on my List<String> dataList so that I could loop through that specific lists' value and alter it.

However, whenever I try to do that,I always get an error of:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.util.List.

Here's a sample of how I am trying to assign a specific list from dataTableList to 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++;
}

EDIT:

Here's my code and I modified some of it and didn't include some so that I could focus on expressing where the problem occurs.

Here's my 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;
    }
}

Then here's my 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
         }
      }
   }
}

来源:https://stackoverflow.com/questions/59745696/java-lang-classcastexception-ljava-lang-object-cannot-be-cast-to-java-util-li

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!