I have 2 classes and 2 interfaces in my project. In my main(), i have Null Pointer Exception on produce() function. I tried to do produce function to static but it didn't work for me well. Here is my codes, what's the problem ?
This is my CarProjectTest.java codes
import java.util.Scanner;
public class CarProjectTest{
private static int velocity;
private static double time;
private double price;
private CarDecorator carDecorator;
public static void main(String[] args){
String carType;
Scanner user_input = new Scanner( System.in );
// TODO initiate factory!
ICarFactory factory = null;
System.out.print("Lütfen üretilecek arabanın modelini giriniz: ");
carType = user_input.next();
System.out.println(carType);
System.out.print("Lütfen ortalama hızı giriniz: ");
velocity = user_input.nextInt();
System.out.println(velocity);
System.out.print("Lütfen zamanı giriniz: ");
time = user_input.nextDouble();
System.out.println(time);
ICar car1 = factory.produce(carType);
System.out.println("Price: " + car1.getPrice());
System.out.println("Message: " + car1.getMessage(90));
System.out.println("Fuel Consumption: " + car1.getFuelConsumption(100, 1.2));
/*car1 = AirConditioningDecorator(car1); // please read the name carefully and think about the hints
System.out.println("Price: " + car1.getPrice()); // Price should increase by 1000 TL
car1 = AutoShiftDecorator(car1);
System.out.println("Price: " + car1.getPrice()); */
}
//functions
public ICar produce(String carType){
if(carType.equals("TN100")){
carDecorator.setEngineVolume(1.33);
carDecorator.setWeight(768);
carDecorator.setPrice(20000);
carDecorator.setFuelConsumption(velocity, time, 5);
}
else if(carType.equals("TN200")){
carDecorator.setEngineVolume(2);
carDecorator.setWeight(900);
carDecorator.setPrice(25000);
carDecorator.setFuelConsumption(velocity, time, 7.2);
}
else if(carType.equals("TN300")){
carDecorator.setEngineVolume(2.4);
carDecorator.setWeight(1100);
carDecorator.setPrice(30000);
carDecorator.setFuelConsumption(velocity, time, 9);
}
return carDecorator.getDecoratedCar();
}
}
and there is my ICarFactory class
public interface ICarFactory {
public ICar produce(String carType);
}
This is the error what i take;
Exception in thread "main" java.lang.NullPointerException at com.project.factory.CarProjectTest.main(CarProjectTest.java:25)
25th line is: ICar car1 = factory.produce(carType); in Test class