Search code examples
nullpointerexceptionfactory-pattern

Implementing Factory Pattern and Gettingt NullPointerException, unable to figure out can somebody help me to fix


I am posting this code in which I implemented factory method,Code is compiled successfully but null pointer exception occurs at run time and I am unable to figure it out, I know when an object reference points to nothing then this Exception occurs but in my case I am unable to figure out,Can somebody suggest me the fix here I am posting the code.

import java.util.Scanner;
abstract class Plan
{
 protected double rate;

public abstract void getRate();

public void calculateBill(int totalUnitsConsume)
{
    System.out.println(totalUnitsConsume*rate);
}

}   


  class DomesticPlan extends Plan
  {
   public void getRate()
   {
    rate=3.5;
    }
  }

 class CommercialPlan extends Plan
 {
public void getRate()
 {
 rate=7.50;
 }

 }

class InstitutionalPlan extends Plan
 {
 public void getRate()
 {
  rate=5.50;
  }

  }

 class PlanFactory
 {
  public static Plan getPlan(String planType)
  {
  if(planType==null)
 {
  return null;
  }

   if (planType.equalsIgnoreCase("DOMESTIC PLAN"))
  {
   return new DomesticPlan();
  }
   else if(planType.equalsIgnoreCase("COMMERCIAL PLAN"))   
    { 
     return new CommercialPlan();
      }
       else if (planType.equalsIgnoreCase("INSTITUTION PLAN"))
        {
        return new InstitutionalPlan();
          }



     return null;

    }
    }


    public class ElectricityBill
    {
       public static void main(String ... qwe)
     {

     int choice;
    System.out.println("Choose The Plan type");
    System.out.println("1.Domestic Plan");
    System.out.println("2.Commercial Plan");
    System.out.println("3.Institution Plan");

    Scanner sc=new Scanner(System.in);

    choice=sc.nextInt();
    if(choice>3)
    {
      System.out.println("Wrong choice entered");
    }
    else
    {
     System.out.println("You chose Plan no.:"+ choice);
    }
     switch(choice)
    {
    case 1 : Plan p=PlanFactory.getPlan("DomesticPlan");
             p.getRate();
             p.calculateBill(100);  
             break;

   case 2 : Plan p2=PlanFactory.getPlan("CommercialPlan");
            p2.getRate(); 
            p2.calculateBill(300);
            break;

   case 3 : Plan p3=PlanFactory.getPlan("InstitutionalPlan");
            p3.getRate();  
            p3.calculateBill(250); 
            break;
  default : System.out.println("May be you entered wrong choice");
       }



       }
       }
 Output

 I:\Data>java ElectricityBill
 Choose The Plan type
 1.Domestic Plan
 2.Commercial Plan
 3.Institution Plan
 1
 You chose Plan no.:1
 Exception in thread "main" java.lang.NullPointerException
    at ElectricityBill.main(ElectricityBill.java:96)

Solution

  • 51. planType.equalsIgnoreCase("DOMESTIC PLAN")
    ...
    96. PlanFactory.getPlan("DomesticPlan")
    

    Here none of the if...else block is getting executed, thus getPlan is returning null.

    Please change "DomesticPlan" to "DOMESTIC PLAN" in line 96