So I am doing a program that works by having inputing a price and then using that price along with set values to do a calculation across two child classes that inherit from a superclass where a class is called there which is where the price is kept the issue I have is that whenever I call upon the payment calculation method I get a NullPointerException
package Lab1;
import java.util.Scanner;
public class main{
public static void main(String[] args){
Car c = new Car("model",1998, 123);
ThirdPartyPolicy tp = new ThirdPartyPolicy("hello");
ComprehensivePolicy comp = new ComprehensivePolicy(2,31);
Scanner scan = new Scanner(System.in);
System.out.println("Input price of car");
c.price= scan.nextDouble();
tp.printtp();
comp.printcp();
comp.calcPayment();
}
}
class Car{
String model;
int ManufacturingYear;
double price;
//constructor
public Car(String model, int ManufacturingYear, int price){
this.model=model;
this.ManufacturingYear=ManufacturingYear;
this.price=0;
}
enum CarType {
SUV,
SED,
LUX,
HATCH,
ETC,
}
public double getprice(){
return price;
}
}
abstract class InsurancePolicies{
String policyHolderName;
int id;
Car car;
int numberofclaims = 1;
double premium;
int flatRate = 150;
double calcPayment(){
this.premium = ((car.price/100)+(numberofclaims*200)+flatRate);
return premium;
}
public void print(){
System.out.println(premium);
}
}
class ThirdPartyPolicy extends InsurancePolicies{
String comments;
public ThirdPartyPolicy(String comments){
this.comments=comments;
}
@Override
public double calcPayment(){
this.premium = ((car.price/100)+(numberofclaims*200)+flatRate);
System.out.println( premium);
return premium;
}
public void printtp(){
super.print();
}
}
class ComprehensivePolicy extends InsurancePolicies{
int driverage;
int level;
public ComprehensivePolicy(int level, int driverage){
this.level=level;
this.driverage=driverage;
}
public Car getCar() {
return car;
}
@Override
double calcPayment(){
this.premium = ((car.price/100)+(numberofclaims*200)+flatRate);
if (driverage<30){
this.premium += ((30-driverage)*50);
return premium;
}
return premium;
}
public void printcp(){
super.print();
}
}
If anyone can help me resolve this issue that would be appreciated