Unable to iterate over list

Go To StackoverFlow.com

0

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;
}
2012-04-04 20:06
by Doc Holiday
WHAT error? Also, throws Exception is not very sensible - Niklas B. 2012-04-04 20:07
The error in the titl - Doc Holiday 2012-04-04 20:08
Can only iterate over an array or an instance of java.lang.Iterabl - Doc Holiday 2012-04-04 20:08


5

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

protected final List<ItemBean> buildItemFilterList( 
     HttpServletRequest request,
     CommonDAO dao,
     List<ItemBean> list
)
2012-04-04 20:09
by SWoeste
hmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm - Doc Holiday 2012-04-04 20:10
error went away....... - Doc Holiday 2012-04-04 20:18


0

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

2012-04-04 20:09
by ahanin


0

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.

2012-04-04 20:10
by Wei Ma
list <> sosList, that access is legit : - SWoeste 2012-04-04 20:18
Sorry! I thought it was a typo in the variable name : - Wei Ma 2012-04-04 20:26
Ads