validating when resultList is null

I have following method in service and when I am calling it, the method is stopping here:


            if(materialBalance != null){

When I debugged it, i see it is returning null value but it is terminated (cursor is not moving forward after the above line). Not sure where is the problem, here is the full code:


public void materialBalanceUpdate(Material material, Warehouse warehouse, BigDecimal quantity, String operation){
        try (Transaction tx = persistence.createTransaction()) {

            TypedQuery<MaterialBalance> query2 = persistence.getEntityManager().createQuery("select e from erp$MaterialBalance e " +
                    "where e.material.id = ?1 and e.warehouse.id = ?2", MaterialBalance.class);
            query2.setParameter(1, material.getId());
            query2.setParameter(2, warehouse.getId());
            MaterialBalance materialBalance = query2.getFirstResult();
            if(materialBalance != null){
                if (operation.equalsIgnoreCase("add")) {
                    materialBalance.setBalanceQuantity(materialBalance.getBalanceQuantity().add(quantity));
                } else {    //subtract
                    materialBalance.setBalanceQuantity(materialBalance.getBalanceQuantity().subtract(quantity));
                }
            }else {

                MaterialBalance materialBalance1 = metadata.create(MaterialBalance.class);
                materialBalance1.setMaterial(material);
                //materialBalance1.setWarehouse();
                if (operation.equalsIgnoreCase("add")) {
                    materialBalance1.setBalanceQuantity(quantity);
                } else {    //subtract
                    materialBalance.setBalanceQuantity(new BigDecimal(0).subtract(quantity));
                }
                persistence.getEntityManager().persist(materialBalance1);
            }
            tx.commit();
        }
    }