null pointer exception - data manipulation

I am trying to update existing datasource by comparing it with another datasource but getting an exception.

my code


  private void loadPurchaseOrderDetails(){

        // create a LoadContext
        LoadContext<PurchaseOrderLine> loadContext = LoadContext.create(PurchaseOrderLine.class)
                .setQuery(LoadContext.createQuery("select e from erp$PurchaseOrderLine e " +
                        "where e.purchaseOrder.id = :poid")
                        .setParameter("poid", materialReceiptVendorDs.getItem().getPurchaseOrder().getId()))
                .setView("purchaseOrderLinePo-view"); //View.LOCAL or "purchaseRequisitionLinePR-view"
        // load list using DataManager
        List<PurchaseOrderLine> purchaseOrderLines = dataManager.loadList(loadContext);
        BigDecimal receivedQty;
        for (PurchaseOrderLine line : purchaseOrderLines) {
            receivedQty = BigDecimal.ZERO;
            if(PersistenceHelper.isNew(getItem())) {
                MaterialReceiptVendorLine mr = metadata.create(MaterialReceiptVendorLine.class);
                mr.setMaterial(line.getMaterial());
                mr.setUnitOfMeasure(line.getUnitOfMeasure());
                //  mr.setQuantity(line.getQuantity().subtract(line.getReceivedQuantity()));
                mr.setMaterialReceiptVendor(getItem());
                mr.setPurchaseOrder(line.getPurchaseOrder());
                mr.setPurchaseOrderLine(line);

                if (line.getReceivedQuantity() != null) {
                    receivedQty = line.getReceivedQuantity();
                } else {
                    receivedQty = line.getReceivedQuantity();
                }


                mr.setPoBalance(line.getQuantity().subtract(receivedQty));

                materialReceiptVendorLineDs.addItem(mr);
            }else{
                for(MaterialReceiptVendorLine line1 : materialReceiptVendorLineDs.getItems()){
                       if(line.getId().equals(line1.getPurchaseOrderLine().getId())) {
                            if(line.getQuantity()==null){
                                materialReceiptVendorLineDs.getItem().setPoBalance(line.getQuantity().subtract(line.getReceivedQuantity()));
                            }else{

                                BigDecimal qty = new BigDecimal(100);
  //                              materialReceiptVendorLineDs.getItem().setPoBalance(line.getQuantity().add(line1.getQuantity()).subtract(line.getReceivedQuantity()));
                                materialReceiptVendorLineDs.getItem().setPoBalance(line.getQuantity());

                            }

                     }
                }

            }
        }

    }

The exception is thrown from the following line of code above


           materialReceiptVendorLineDs.getItem().setPoBalance(line.getQuantity());

exception:


java.lang.NullPointerException
	at com.company.web.mm.materialreceiptvendor.MaterialReceiptVendorEdit.loadPurchaseOrderDetails(MaterialReceiptVendorEdit.java:233)
	at com.company.erp.web.mm.materialreceiptvendor.MaterialReceiptVendorEdit.postInit(MaterialReceiptVendorEdit.java:125)
	at com.haulmont.cuba.gui.components.AbstractEditor.setItem(AbstractEditor.java:70)
	at com.haulmont.cuba.gui.WindowManager.openEditor(WindowManager.java:721)
	at com.haulmont.cuba.web.WebWindowManager.openEditor(WebWindowManager.java:157)
	at com.haulmont.cuba.gui.components.WindowDelegate.openEditor(WindowDelegate.java:250)
	at com.haulmont.cuba.web.gui.WebWindow.openEditor(WebWindow.java:444)
	at com.haulmont.cuba.gui.components.actions.EditAction.internalOpenEditor(EditAction.java:232)
	at com.haulmont.cuba.gui.components.actions.EditAction.actionPerform(EditAction.java:225)
	at com.haulmont.cuba.web.gui.components.WebButton.performAction(WebButton.java:44)

You write:


if(line.getQuantity()==null){
 materialReceiptVendorLineDs.getItem().setPoBalance(line.getQuantity().subtract(line.getReceivedQuantity()));

It certainly produces NPE because line.getQuantity() is null.

Hi Konstantin
I’m sorry, the code I shared with you is obviously suppose to through exception when the value is null. But what I noticed while debugging that when the “line.getQuantity()” is not null, I am getting exceptions in the following line (not the line under “if(line.getQuantity()==null)”:


materialReceiptVendorLineDs.getItem().setPoBalance(line.getQuantity());

Is there any error here?

Hi Konstantin
I have spend more time on it and still wondering where is the error.
here is the error code


 at com.vaadin.ui.Button$1.click(Button.java:54)
        ... 44 more
Caused by: java.lang.NullPointerException
        at com.company.erp.web.mm.materialreceiptvendor.MaterialReceiptVendorEdit.loadPurchaseOrderDetails(MaterialReceiptVendorEdit.java:235)
        at com.company.erp.web.mm.materialreceiptvendor.MaterialReceiptVendorEdit.postInit(MaterialReceiptVendorEdit.java:125)
        at com.haulmont.cuba.gui.components.AbstractEditor.setItem(AbstractEditor.java:70)
        at com.haulmont.cuba.gui.WindowManager.openEditor(WindowManager.java:721)

i have attached the snapshot of the code where you will see the content of line 235 where the error is reported above

the field poBalance is a transient field in the entity and i have defined it as follows:


 @Transient
    @MetaProperty
    protected BigDecimal poBalance;

  public void setPoBalance(BigDecimal poBalance) {
        this.poBalance = poBalance;
    }

    public void setQuantity(BigDecimal quantity) {
        this.quantity = quantity;
    }

error5

Probably yourCollectionDs.getItem() method returns null. It can be if there is no selected row in the linked table.