[英]Why am I getting a null pointer exception when passing an ArrayList of strings from one activity to another?
[英]Why am I getting null pointer exception when I add an item to ArrayList
我的處方課如下:
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;
}
}
我的測試設置如下:
我的設置方法如下:
@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);
}
在將prescriptionItems
添加到prescriptionProduct
位置處收到NullPointerException
。 有什么幫助嗎?
我嘗試在我的計算機中重現您的代碼並且它工作正常,添加了一個產品並使用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"
}
}
嘗試初始化處方項 ArrayList :
private ArrayList <Product> prescriptionItems;
到
private ArrayList <Product> prescriptionItems = new ArrayList<Product>();
當您在測試代碼中聲明它時。
順便說一下,如果您想通過處方類中的addPrescriptionItem方法測試添加產品,該方法將產品添加到已在 Prescription 類中定義的 ArrayList,為什么要為產品添加新的 ArrayList。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.