将对象添加到列表时获取NullPointerException [重复]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将对象添加到列表时获取NullPointerException [重复]相关的知识,希望对你有一定的参考价值。
这个问题在这里已有答案:
我非常需要帮助。尝试4-5个小时后,我发现自己迷路了。我知道有2个其他线程具有相同的标题,但那些没有帮助。 T.T
我不知道如何解决它,我现在疯了。所以我希望有人可以帮我解决这个问题。
如果有人可以帮我解决这个问题并向我解释我做错了什么,所以我将来不再这样做,我会很高兴。
我有3个类,我正在尝试使用OrderMapper中的Main函数制作一个shopcart,也就是CustomerOrder对象。然后通过列表的添加函数将OrderDetails对象添加到它的List中,但我一直得到NullPointerException。
OrderDetails类:
package model.domain;
public class OrderDetails {
private int id;
private int bookid;
private int amount;
public OrderDetails() {
}
public OrderDetails(int id, int bookid, int amount) {
this.id = id;
this.bookid = bookid;
this.amount = amount;
}
public OrderDetails(int bookid, int amount) {
this.bookid = bookid;
this.amount = amount;
}
public int getId() {
return id;
}
public int getBookid() {
return bookid;
}
public void setBookid(int bookid) {
this.bookid = bookid;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
}
CustomerOrder类:
package model.domain;
import java.util.Date;
import java.util.List;
public class CustomerOrder {
private int id;
private int customerid;
private List<OrderDetails> orderDetails;
public CustomerOrder() {
}
public CustomerOrder(int id, int customerid, List<OrderDetails> orderDetails) {
this.id = id;
this.customerid = customerid;
this.orderDetails = orderDetails;
}
public CustomerOrder(int customerid, List<OrderDetails> orderDetails) {
this.customerid = customerid;
this.orderDetails = orderDetails;
}
public int getId() {
return id;
}
public int getCustomerid() {
return customerid;
}
public void setCustomer(int customerid) {
this.customerid = customerid;
}
public List<OrderDetails> getOrderDetails() {
return orderDetails;
}
public void setOrderDetails(List<OrderDetails> orderDetails, OrderDetails od) {
orderDetails.add(od);
this.orderDetails = orderDetails;
}
public void addDetails(int bookid, int amount){
OrderDetails od = new OrderDetails();
od.setBookid(bookid);
od.setAmount(amount);
this.orderDetails.add(od);
}
// public void addDetails(OrderDetails od){
// this.orderDetails.add(od);
// }
//addOrderline /addDetails
//
//od.setOne(this.ono)
//orderdetails.add(ad)
//arraylist<orderdetail> getDetails(){
// return orderDetails;
//}
}
OrderMapper类:
package model.domain;
import java.util.ArrayList;
import java.util.List;
public class OrderMapper {
public CustomerOrder createShopcart(int customerid, List<OrderDetails> orderDetails){
return (new CustomerOrder(customerid, orderDetails));
}
public static void main(String[] args) {
OrderMapper om = new OrderMapper();
OrderDetails ed = new OrderDetails(1, 2);
//List<OrderDetails> orderDetails = null;
// // orderDetails = ed;
//orderDetails.add(0, ed));
CustomerOrder shopcart = new CustomerOrder();
System.out.println(shopcart.getOrderDetails());
shopcart.setCustomer(1);
shopcart.addDetails(1, 3);
shopcart.addDetails(3, 2);
shopcart.addDetails(2, 2);
System.out.println(shopcart.getOrderDetails());
// CustomerOrder shopcart = om.createShopcart(1, null);
// System.out.println(shopcart.getOrderDetails());
// OrderDetails od = new OrderDetails(1, 2);
// shopcart.setOrderDetails(null, od);
// System.out.println(shopcart.getOrderDetails());
// shopcart.setCustomer(1);
// shopcart.addDetails(1, 1);
}
}
我得到的错误是我知道第一个null来自我设置的null,但后来我添加了它,它得到了NullPointerException。我尝试过很多东西,但没有人帮忙:
null
Exception in thread "main" java.lang.NullPointerException
at model.domain.CustomerOrder.addDetails(CustomerOrder.java:68)
at model.dataaccess.OrderMapper.main(OrderMapper.java:155)
答案
你永远不会给你的对象一个列表...因此当你调用shopcart.addDetails(1,1);您将获得空指针异常,因为您的shopcart对象永远不会使用有效的列表对象进行实例化。
CustomerOrder shopcart = new CustomerOrder(); //no list given
尝试使用您创建的函数调用createShopcart(int customerid,List orderDetails)
CustomerOrder shopcart = new CustomerOrder(0, new ArrayList<OrderDetails>());
以上是关于将对象添加到列表时获取NullPointerException [重复]的主要内容,如果未能解决你的问题,请参考以下文章