简体   繁体   中英

Unable to iterate over list

I'm appending to a list then throwing that list into a function below:

 List<ItemBean> itemList;

        for (Object bean : beans)
        {
            if (!bean.getItem().isActive())
            {
                itemList.add(bean.getItem().getId());
            }
       }

       if (!itemList.isEmpty())
       {
        // Source of Supply List
        buildItemList( request, commonDAO, false );
       }
       else{
           buildItemFilterList( request, commonDAO, itemList);

       }

But I'm getting "Can only iterate over an array or an instance of java.lang.Iterable" error here:

 protected final List<ItemBean> buildItemFilterList( HttpServletRequest request,
                                                             CommonDAO dao,
                                                             ItemBean list
                                                            )
    throws Exception
{
    List<ItemBean> itemList = dao.getAllItems( false );
    ItemBean item;

    for (ItemBean s: list )  <<<<<-----ERROR!!!!!!!!!!!
    {
        item = dao.getItemById(s.getId());
        itemList.add(item);
    }

    Collections.sort( itemList );
    request.setAttribute("itemList", itemList);
    return itemList;
}

The error is, that your argument "SourceOfSupplyBean list" is not a collection.

protected final List<ItemBean> buildItemFilterList( 
     HttpServletRequest request,
     CommonDAO dao,
     List<ItemBean> list
)

Apparently, parameter 'list' is of type of ItemBean and ItemBean is not an instance of Iterable or an array.

protected final List<ItemBean> buildItemFilterList( HttpServletRequest request,
                                                             CommonDAO dao,
                                                             ItemBean list
                                                            )
    throws Exception
{
    List<ItemBean> itemList = dao.getAllItems( false );
    ItemBean item;

    for (ItemBean s: list )  <<<<<-----ERROR!!!!!!!!!!!
    {
        item = dao.getItemById(s.getId());
        itemList.add(item); <<< --real error is here.
    }

    Collections.sort( itemList );
    request.setAttribute("itemList", itemList);
    return itemList;
}

You cannot modify a list while iterating through it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM