实施工厂模式和 Gettingt NullPointerException,无法弄清楚有人可以帮我解决
Implementing Factory Pattern and Gettingt NullPointerException, unable to figure out can somebody help me to fix
我贴出这段我实现了工厂方法的代码,代码编译成功但是在运行时出现了空指针异常,我无法弄清楚,我知道什么时候对象引用指向然后什么都没有发生这个异常但在我的情况下我无法弄清楚,有人可以在这里建议我修复我发布代码。
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)
您错过了“DomesticPlanin your code, so your factory can not match any string and returns null, just correct it to
Domestic Plan”中的 space
那是因为你的工厂在这种情况下没有返回计划。您正在执行 PlanFactory.getPlan("DomesticPlan")
(DomesticPlan 中没有 space),但在您的 getPlan 方法中您正在寻找 "DOMESTIC PLAN"(使用 space),因此 Plan p
为空。之后立即抛出异常,您在空计划上调用 getRate
方法。
当您从工厂获取值时,您应该进行 null 检查,例如if (p != null) { p.getRate(); ... }
51. planType.equalsIgnoreCase("DOMESTIC PLAN")
...
96. PlanFactory.getPlan("DomesticPlan")
if...else
块的 none 正在执行,因此 getPlan
返回 null
。
请将第 96 行的 "DomesticPlan"
更改为 "DOMESTIC PLAN"
我贴出这段我实现了工厂方法的代码,代码编译成功但是在运行时出现了空指针异常,我无法弄清楚,我知道什么时候对象引用指向然后什么都没有发生这个异常但在我的情况下我无法弄清楚,有人可以在这里建议我修复我发布代码。
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)
您错过了“DomesticPlanin your code, so your factory can not match any string and returns null, just correct it to
Domestic Plan”中的 space
那是因为你的工厂在这种情况下没有返回计划。您正在执行 PlanFactory.getPlan("DomesticPlan")
(DomesticPlan 中没有 space),但在您的 getPlan 方法中您正在寻找 "DOMESTIC PLAN"(使用 space),因此 Plan p
为空。之后立即抛出异常,您在空计划上调用 getRate
方法。
当您从工厂获取值时,您应该进行 null 检查,例如if (p != null) { p.getRate(); ... }
51. planType.equalsIgnoreCase("DOMESTIC PLAN")
...
96. PlanFactory.getPlan("DomesticPlan")
if...else
块的 none 正在执行,因此 getPlan
返回 null
。
请将第 96 行的 "DomesticPlan"
更改为 "DOMESTIC PLAN"