Search code examples
javaspringiteratorsap-commerce-cloud

How to resolve NoSuchElement: null in Java Sap Hybris?


I create an action that modified a value into one or more items (Order), order in status "wait for manual export" into "export", now when i selected the Orders and click on action button nothing change and into the log i have this error :

ERROR [hybrisHTTP33] [ManualOrderExportAction] 
ERROR: java.util.NoSuchElementException: null
at java.util.Collections$EmptyIterator.next(Collections.java:4210) ~[?:?]
at myExtension.backoffice.actions.ManualOrderExportListAction.perform(ManualOrderExportListAction.java:37) [?:?]

Code:

@Resource(name = "businessProcessService")
private BusinessProcessService businessProcessService;

@Override
public ActionResult<Object> perform(ActionContext<Object> actionContext){
    HashSet<OrderModel> hashSet = (HashSet<OrderModel>) actionContext.getData();
    try
    {
        businessProcessService.triggerEvent(hashSet.iterator().next().getOrderProcess().iterator().next().getCode() + "_" + "SaitManualExportEvent" ); //line 37 that generate error.
        Messagebox.show(POPUP_MESSAGE);
        return new ActionResult<>(ActionResult.SUCCESS);
    }
        catch (final Exception ex)
    {
        LOG.error("ERROR: ", ex);
        return new ActionResult<>(ActionResult.ERROR);
    }

}

Solution

  • The below lines:

    ...
    businessProcessService.triggerEvent(hashSet.iterator().next().getOrderProcess().iterator().next().getCode() + "_" + "SaitManualExportEvent" ); //line 37 that generate error.
    Messagebox.show(POPUP_MESSAGE);
    return new ActionResult<>(ActionResult.SUCCESS);
    ...
    

    should be changed to, something like:

    ...
    if (hashSet.iterator().hasNext())
    {
        // NOTE: Don't know the data type returned by getOrderProcess(), so
        //       assumed a dummy data type OrderProcess 
        OrderProcess o = hashSet.iterator().next().getOrderProcess();
        if (o.iterator().hasNext())
        {
            businessProcessService.triggerEvent(o.iterator().next().getCode() + "_" + "SaitManualExportEvent" );
            Messagebox.show(POPUP_MESSAGE);
            return new ActionResult<>(ActionResult.SUCCESS);
        }
    }
    ...