0

I have followed relevant links for nullpointerexception such as :

How to solve java.lang.NullPointerException error?

What is a NullPointerException, and how do I fix it?

I got most of my nullpointer exceptions sorted out from the code but I am unable to get the reason for following nullpointer exception.

 Exception in thread "main" java.lang.NullPointerException
      at Calculate.main(Calculate.java:9)

I ran in debug mode and saw "sc.items.add(cd)" ( line 9 of Calculate.java); is null! Can anyone explain the reason.

I am implementing a visitor pattern. I am calculating the cost of the different items. One item is CD. Below is the code:

ShoppingCart.java

   import java.util.ArrayList;

   public class ShoppingCart {

     ArrayList<Visitable> items;

     public double calculatePostage(){
        //Create a visitor

        PostageVisitor visitor = new PostageVisitor();

     // iterate through all items
          for( Visitable item: items){

             item.accept(visitor);
         }

        double postage = visitor.getTotalPostage();

        return postage;

     }
 }

Calculate.java

 public class Calculate {

      public static void main(String[] args) {
          // TODO Auto-generated method stub
        ShoppingCart sc = new ShoppingCart();

        CD cd = new CD(100);
        sc.items.add(cd);   
       double postageInMain =  sc.calculatePostage(); 
       System.out.println(postageInMain);

      }

}

CD.java

    public class CD implements Visitable {

         private double price;

         @Override
         public void accept(Visitor visitor) {
            // TODO Auto-generated method stub

     }
      public CD(double price){
            this.price = price;
     }    
     double getPrice(){
          return price;
     }

 }

PostageVisitor.java

    public class PostageVisitor implements Visitor {
      private double totalPostageForCart;



      public void visit (CD cd){

            totalPostageForCart += cd.getPrice(); 
       }

   public void visit (DVD dvd){

       totalPostageForCart += dvd.getPrice();
  }
    // return the internal state
    public double getTotalPostage(){

         return totalPostageForCart;

    }

 }

Visitable.java

  public interface Visitable {
    public void accept(Visitor visitor);
 }

Visitor.java

 public interface Visitor {
     public  void visit(Book book);


    public void visit(CD cd);


}
Community
  • 1
  • 1
amjear
  • 75
  • 1
  • 1
  • 4

2 Answers2

0

Field items in class ShoppingCart is not initialized with ArrayList object, that's why you get NPE.

import java.util.ArrayList;

   public class ShoppingCart {

     ArrayList<Visitable> items = new ArrayList<>(); // <---- this will solve the problem

     public double calculatePostage(){
        //Create a visitor

        PostageVisitor visitor = new PostageVisitor();

     // iterate through all items
          for( Visitable item: items){

             item.accept(visitor);
         }

        double postage = visitor.getTotalPostage();

        return postage;

     }
 }

Also, I would like to mention, that you don't really need public modifiers in interface methods, it is just redundant.

Vasiliy Vlasov
  • 3,316
  • 3
  • 17
  • 20
0

Change your ShoppingCart.java file ArrayList<Visitable> items; code to

ArrayList<Visitable> items;
items = new ArrayList<>();
Isuru Dilshan
  • 719
  • 9
  • 18