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;
}
2
  • WHAT error? Also, throws Exception is not very sensible.
    – Niklas B.
    Commented Apr 4, 2012 at 20:07
  • Can only iterate over an array or an instance of java.lang.Iterable Commented Apr 4, 2012 at 20:08

3 Answers 3

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
)
1
  • hmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm Commented Apr 4, 2012 at 20:10
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.

2
  • list <> sosList, that access is legit :)
    – SWoeste
    Commented Apr 4, 2012 at 20:18
  • Sorry! I thought it was a typo in the variable name :(
    – Wei Ma
    Commented Apr 4, 2012 at 20:26
0

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.