Hello guys, I'm trying to add an object called product into an ArrayList of orders. I'm getting an error: no suitable method found for add(Product). Did I miss anything?
This is my addOrder method:

    public void addOrder(ArrayList<Order> orders, Product product){
        orders.add(product);

    }

I've passed the ArrayList of orders and the object Product product so that I can add the product into the list.

Under my User class, I have:

 public void buyProduct(ArrayList<Product> products, ArrayList<Order> orders, Cart cart){
        Scanner sc = new Scanner(System.in);

        for (int i=0; i<products.size();i++){
            System.out.println("-----------------------------");
            System.out.println(i+1+".");                    
            products.get(i).displayProducts(products);
    }

        System.out.println("-----------------------------");

        System.out.print("Choose what product to buy: ");
        int resp = sc.nextInt();

        System.out.println("-----------------------------");        

        switch(resp){
            case 1: {
               System.out.print("Quantity: ");
               int quantity = sc.nextInt();
               cart.addOrder(orders, products.get(0));
               products.get(0).setStocksLeft(products.get(0).getStocksLeft()-quantity);
            }
                break;

            case 2: {
               System.out.print("Quantity: ");
               int quantity = sc.nextInt();
               cart.addOrder(orders, products.get(1));
               products.get(1).setStocksLeft(products.get(1).getStocksLeft()-quantity);               
            }
                break;

            case 3: {
               System.out.print("Quantity: ");
               int quantity = sc.nextInt();
               cart.addOrder(orders, products.get(2));
               products.get(2).setStocksLeft(products.get(2).getStocksLeft()-quantity);                
            }
                break;

            case 4: {
               System.out.print("Quantity: ");
               int quantity = sc.nextInt();
               cart.addOrder(orders, products.get(3));
               products.get(3).setStocksLeft(products.get(3).getStocksLeft()-quantity);                
            }
                break;

            default: System.out.println("Incorrect input!");    
                break;

        }
    }

The user inputs the correspoding number of the product they would want to purchase. Once they select the product, the user must input the quantity. Then I take the quantity they've inputted and deduct it from the stock.

orders is an arraylist of Orders, so (unless Product is a subclass of Order) you can't add a Product to it.

Okay, so I've made my Product class extend Order, but when I ran the program, I got a NullPointerException error.

What???
Why did you do that?
Is Product a kind of Order? If not, it should not be a subclass.

The question is: what were you trying to do when you coded orders.add(product);?
Did you really want to put a Product object into a list that can only hold Order objects?
Maybe (MAYBE) you intended to create a new Order object for that Product and add tha to the list? I don't know.

ps: YOu've been around on these forums since 2011 and yet you still post "got an NPE" without giving any details such as where it happened? Shame.

My apologies JamesCherrill. Please forgive my ignorance. I'm trying to take the product that the user has chosen and store it in the ArrayList of orders. My goal is to make the ArrayList of orders hold the product orders of the user. I see that making my Product class extend Order is incorrect. I'm trying to make a new Order object for the product that the user has chosen, then store it inside the ArrayList of orders.

I don't have the code for the Order class, but I guess it may have a constructor that takes a Product and a quantity or something like that? Anyway, if you create your new Order object then addding that to the list will work just fine.

Here's what I have under my Order class:

public class Order {

    private int quantity;
    private Product product;

    public Order(int quantity, Product product) {
        this.quantity = quantity;
        this.product = product;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public Product getProducts() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }



}

Just for clarification, should I create my new Order object under my Cart class?

public class Cart {

    public Cart(){

    }

    private ArrayList<Order> orders;

    public Cart(ArrayList<Order> orders) {
        orders = new ArrayList<Order>();
    }

    public ArrayList<Order> getOrders() {
        return orders;
    }

    public void setOrders(ArrayList<Order> orders) {
        this.orders = orders;
    }

    public void addOrder(Product product){
        orders.add(product());
    }

    public void removeOrder(Product product){
        orders.remove(product);
    }

    public void getOrder(){

    }

    public void displayOrders(){
        for(int i = 0; i<orders.size();i++){
            orders.get(i).getProducts().getName();
            orders.get(i).getProducts().getStocksLeft();
        }
    }

    public int getCartSize(ArrayList<Order> orders){
        return orders.size();
    }

    public void emptyCart(ArrayList<Order> orders){
        orders.clear();
    }
}

Btw, I'm getting an error under my addOrder method: no suitable method found for add(Product). I think it's tied up with my problem creating a new Order object for the user's product.

It's exactly the same problem as above. You can't add a Product to a list of Orders. You have to create a new Order and add that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.