I need help, badly. After trying for 4-5 hours, I figured that I'm lost. I know there are 2 other threads that has the same title but those didn't help. T.T
I don't know how to fix it and I'm going crazy now. So I hope someone can help me with this problem.
If someone could help me fix it and explain to me what the heck I did wrong so I don't do it again in the future, I would be damn happy.
I have 3 classes where I'm trying to make the Main function in OrderMapper make a shopcart aka an CustomerOrder object. Then add an OrderDetails object to it's List through add function for lists but I keep getting a NullPointerException.
OrderDetails class:
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 class:
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 class:
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);
}
}
The error I get is this where I know the first null is from the null I set but then I add and it gets me the NullPointerException. I tried many things but none helped:
null
Exception in thread "main" java.lang.NullPointerException
at model.domain.CustomerOrder.addDetails(CustomerOrder.java:68)
at model.dataaccess.OrderMapper.main(OrderMapper.java:155)