0

I have three classes Product, Cart, Shop. Cart class inherits with Product class. Shop class has the main function and I am trying to create an object of Cart class. but it's not getting created. It's my homework assignment they mention that I have to use ArrayList in Cart class and create an object of Cart in Shop Class. if I inherit Cart class with Shop class public class Shop extends Cart{ .... then I can use Cart class Methods but I can't submit it like this I have to create an object of Cart in Shop and have to use it

Error:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Cart.a
ddItem(Product)" because "this.myCart" is null
        at BookShop.addItem(BookShop.java:109)
        at BookShop.showGUI(BookShop.java:42)
        at BookShop.main(BookShop.java:10)

here are the classes these are a piece of code, not the whole program.

Product.java

public class Product {
    private String name;
    private int quantity;

    public Product(){
        System.out.println("Object Created");
        name = null;
        quantity = 0;
    }
    
    public Product(String name,int quantity){
        this.name = name;
        this.quantity = quantity;
    }
    public Product(Product p){
        this.name = p.name;
        this.quantity = p.quantity;
    }
    public void setName(String Name){
        this.name = Name;
    }
    public void setQuantity(int quantity){
        this.quantity = quantity;
    }
    public String getName(){
        return this.name;
    }
    public int getQuantity(){
        return this.quantity;
    }
}

Cart.java

import java.util.ArrayList;

public class Cart extends Product{
    private ArrayList<Product> CartItems;

    public Cart(){
        super();
        System.out.println("Object Created");
        CartItems = new ArrayList<Product>();
        

    }
    public Cart(String name,int quantity){
        super(name,quantity);
    }
    public Cart(Cart c){
        this.CartItems = c.CartItems;
    }
    public void addItem(Product p){
        this.CartItems.add(p);
    }
    public void setCart(ArrayList<Product> cart){
        CartItems = cart;
    }
    public ArrayList<Product> getList(){
        return this.CartItems;
    }
}

Shop.java

import javax.swing.JOptionPane;

public class Shop{
    public Cart myCart;
    public static void main(String[] args) {
        Shop myShop = new Shop();
        myShop.showGUI();
    }
    public Shop(){
        
    }
    public Shop(Shop s){
        this.myCart = s.myCart ;
    }

    public Shop(Cart c){
        this.myCart.setCart(c.getList());
    }

    public void showGUI(){
        
        String input= JOptionPane.showInputDialog("Enter Quantity");
        Integer qualtiy = Integer.parseInt(input);
        Product p = new Product("Book", qualtiy);

        this.myCart.addItem(p);
        }
    

}
umair mehmood
  • 529
  • 2
  • 5
  • 17
  • In the `Shop` class at `this.myCart.addItem(p);` You don't seem to ever assign anything to `myCart`. – 001 Nov 24 '20 at 18:43
  • 1
    Have you read the exception's message? `this.myCart` is `null` because you never set it to a non-null value anywhere. – f1sh Nov 24 '20 at 18:43
  • Problem filex I added a line in Shop Constructor `myCart = new Cart()` – umair mehmood Nov 24 '20 at 19:17

0 Answers0