简体   繁体   中英

Why am I getting null pointer exception when I add an item to ArrayList

I have my Prescription class as follows:

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);
    }
}

I have my Product class as follows

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;
       }
}

I have my test setup as follows:
I have my setUp method as follows:

        @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);
    }

I have my JUnit/Mockito test as follows:

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

    }

Am getting NullPointerException at where prescriptionItems is added to prescriptionProduct . Any help?

I tried to reproduce your code in my computer and it worked, added a product and returned it using List<Product> getPrescriptionItems() flawlessly.

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"
    }
}

Here is the output: 输出

try initializing prescriptionItems ArrayList :

private ArrayList <Product> prescriptionItems;

to

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

when you declared it in the test code.

By the way why you adding a new ArrayList for products if you want to test adding product through addPrescriptionItem method inside prescription class which adds the product to the ArrayList already defined inside Prescription class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM