-1

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)
  • Hi Blogbeard, I've read that but I still don't get where I go wrong. I cannot seem to fix my code even after trying different stuff. :| – user9120338 Dec 20 '17 at 00:29
  • `new CustomerOrder()` leaves `orderDetails` set to `null` and `id` and `customerid` to `0`. You should probably just delete that constructor and always use the other constructor which properly initializes these fields. – dimo414 Dec 20 '17 at 00:31
  • Or do `private List orderDetails = new ArrayList<>();` so the field is initialized to a non-null value by default. – dimo414 Dec 20 '17 at 00:32
  • Thanks @dimo414, it was the adding new ArrayList<>() that I didn't get right. I should learn from this and only make the constructors I use and learn more about Lists and ArrayLists. – user9120338 Dec 20 '17 at 00:44
  • *only make the constructors I use* - a very good lesson indeed :) in general, don't create *anything* unless you have a concrete reason to need it. – dimo414 Dec 20 '17 at 01:29

1 Answers1

1

You never give your objects a list... Therefore when you call shopcart.addDetails(1, 1); you will get a null pointer exception because your shopcart object never gets instantiated with a valid list object.

CustomerOrder shopcart = new CustomerOrder(); //no list given

Try using the function you created call createShopcart(int customerid, List orderDetails) like this

CustomerOrder shopcart = new CustomerOrder(0, new ArrayList<OrderDetails>());
RAZ_Muh_Taz
  • 4,059
  • 1
  • 13
  • 26
  • Hi @RAH_Muh_Taz, thank you very much. It was a mix of a syntax and newbeginner error. I did try with new ArrayList but never added the () at the end. It looked weird and I deleted it. Thanks for helping me out. So what you are saying that when I instantiate my shopcart, I should remember the valid list object. It cannot be added later to the CustomerOrder object? – user9120338 Dec 20 '17 at 00:40
  • you can add it later but you would have to do more checking in other methods which check if your list != null which is extra unneeded work. your CustomerOrder contains a list as part of its member variables, it's best to give it a list at creation to avoid those null pointer exceptions. then later you could replace the list if you wanted with anotehr function but on creation you want to make sure everything in your class gets initialized properly – RAZ_Muh_Taz Dec 20 '17 at 16:35
  • Cool, thanks. It seems like I should read more about on that stuff. It was something that made me go crazy. After you people helped me, I continued like it was nothing. I just hope that I don't forget this lesson and remember it. Thanks again for the help. :) – user9120338 Dec 20 '17 at 22:32