-4

when I run it display this error Exception in thread "main" java.lang.NullPointerException at Abstract2.main(Abstract2.java:70) pleae tell me how to resolve it

my code is given below

import java.util.*;
abstract class plan
{
public double rate,bill;
public abstract void getrate();
public void cbill(int unit)
{
double bill=rate*unit;
System.out.println("bill is="+bill);
}
}
class domestic extends plan
{
public void getrate()
{
rate=2.5;
}
}
class commercial extends plan
{
public void getrate()
{
rate=5.5;
}
}
class industrial extends plan
{
public void getrate()
{
rate=7.5;
}
}
class bill
{
public plan getplan(String plantype)
{
if(plantype==("domestic"))
{
return new domestic();
}
if(plantype==("commercial"))
{
return new commercial();
}
if (plantype==("industrial"))
{
return new industrial();
}
return null;
}
}
class Abstract2
{
public static void main(String aa[])
{
plan p;//instance of class plan
String plantype;
int unit;
double rate;
bill b=new bill();
Scanner sc=new Scanner(System.in);
System.out.println("enter the plan type=");
System.out.println("     domestic");
System.out.println("     commercial");
System.out.println("     industrial");
plantype=sc.next();
System.out.println("enter the units");
unit=sc.nextInt();
p=b.getplan(plantype);
p.getrate();//line 70
p.cbill(unit);
}

}

please tell me how to resolve it

3 Answers3

2

There are problems in your codes:

  • violate java naming convention, e.g. class name should be capitalized ..
  • getter/setter also violates the convention. your getrate() is void? to get what then?
  • compare String with == , should be equals
  • p=b.getplan(plantype); your getplan(plantype) method could return null. next line (line 70) you didn't check it (p), just simply p.getrate(); potential NPE.
  • the String plantype was not initialized.
Kent
  • 189,393
  • 32
  • 233
  • 301
  • One more correction, in user source he has written `plan p;//instance of class plan` , it should be `plan p;//reference of class plan` :D – Mustafa sabir Jun 17 '14 at 12:28
0

Change method as

public plan getplan(String plantype)
{
if(plantype.equals("domestic"))
{
return new domestic();
}
if(plantype.equals("commercial"))
{
return new commercial();
}
if (plantype.equals("industrial"))
{
return new industrial();
}
return null;
}
}
वरुण
  • 1,237
  • 3
  • 18
  • 50
0

In addition to everything that Kent said, the straight forward answer to your problem is that you never initialized plantype, only declared it. The logic in the getplan() method can prevent it from getting a value. Therefore, it has no concrete value.

A few other things to be aware of:

  1. You really should learn to indent/space out your code. Had to spend a few minutes just to tab it out so I could read it easily. Group things into sections by similar functions (i.e. put all variables declarations together, put all System.out.println together, etc.)
  2. Make your variable names more descriptive. p doesn't explain what it is unless you find the line it was created on.
  3. get...() named functions typically return a value. This should be named to something else.