将项目添加到ArrayList时为什么会出现空指针异常

时间:2020-03-07 20:58:57

标签: java arraylist junit mockito

我的Prescription类如下:

import java.util.ArrayList;
import java.util.List;

public class Prescription {

    private final ArrayList<Product> prescriptionItems = new     ArrayList<Product>();
    private long prescriptionId;

    public List<Product> getPrescriptionItems() {
        return prescriptionItems;
    }

    public void addPrescriptionItem(final Product product) {
        this.prescriptionItems.add(product);
    }
}

我的产品类别如下

public class Product {

    public Product(final long productCode, final double productCost) {
        this.productCode = productCode;
        this.productCost = productCost;
    }


    private final long productCode;
    private final double productCost;

    public long getProductCode() {
        return productCode;
    }

      public double getProductCost() {
        return productCost;
       }
}

我的测试设置如下:
我的setUp方法如下:

        @BeforeEach
    void setUp() throws Exception 
        {
            medAvailControllerImpl = new MedAvailControllerImpl(medAvailDAO, dispenser, paymentHandlerFactory);
        prescriptionProduct = new Product(789, 44.0);
        customer = new Customer(45678, "Amofa Baffoe", "Claregalway", "kbaffoe@hotmail.com", "paypal", paypalStrategy);
        when(medAvailDAO.getCustomerForId(45678)).thenReturn(customer);
    }

我的JUnit / Mockito测试如下:

    @Test
    public void testOneItemOnPrescriptionSuccess() throws Exception {
        //prescriptionProduct = new Product(789, 44.0);
        prescriptionItems.add(prescriptionProduct);
            when(prescription.getPrescriptionItems()).thenReturn(prescriptionItems);

    }

NullPointerException被添加到prescriptionItems的地方得到prescriptionProduct。有帮助吗?

2 个答案:

答案 0 :(得分:0)

我试图在计算机上重现您的代码,该代码可以正常工作,添加了产品,并使用List<Product> getPrescriptionItems()完美地返回了它。

import java.util.ArrayList; 
import java.util.List;

class Product {
    public Product(final long productCode, final double productCost) {
        this.productCode = productCode;
        this.productCost = productCost;
    }


    private final long productCode;
    private final double productCost;

    public long getProductCode() {
        return productCode;
    }

      public double getProductCost() {
        return productCost;
      }
}  
public class Prescription {

    private final ArrayList<Product> prescriptionItems = new ArrayList<Product>();
    private long prescriptionId;

    public List<Product> getPrescriptionItems() {
        return prescriptionItems;
    }

    public void addPrescriptionItem(final Product product) {
        this.prescriptionItems.add(product);
    }


    public static void main(String[] args) {

        // instantiate Prescription
        Prescription pres = new Prescription();

        // add a product to ArrayList using addPrescriptionItem()
        pres.addPrescriptionItem(new Product(50, 15.0));

        // get the product details from the ArrayList
        Product product = pres.getPrescriptionItems().get(0);

        System.out.println(product.getProductCode()+ " " + product.getProductCost()); // will print "name in sup Class"
    }
}

以下是输出: the output

答案 1 :(得分:0)

尝试初始化divisionItems ArrayList:

private ArrayList <Product> prescriptionItems;

private ArrayList <Product> prescriptionItems = new ArrayList<Product>(); 

在测试代码中声明时。

如果要通过 prescription 类中的 addPrescriptionItem 方法测试添加产品的方式添加产品的新ArrayList的方式,该方法会将产品添加到已定义的ArrayList中在Prescription类中。

相关问题