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);


}
1
  • 2
    ArrayList<Visitable> items; is not initialized.... Commented Apr 20, 2017 at 19:33

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

ArrayList<Visitable> items;
items = new ArrayList<>();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.