I am trying to build a garage in java. I have got a NullPointerException when I call the method addOption on my object Lagouna in my Main "Test". It is strange because it works when I call my method setMoteur. My object Lagouna is not null. My object GPS is not null as well.
Main
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Garage garage = new Garage();
System.out.println(garage);
Vehicule lag1 = new Lagouna();
lag1.setMoteur(new MoteurEssence("150 Chevaux", 10256d));
lag1.addOption(new GPS()); // here is my error
}
}
Class Vehicule
public abstract class Vehicule {
protected double prix;
protected String nom;
protected Marque nomMarque;
protected List<Option> options;
protected Moteur moteur;
Vehicule(double prix,String nom,Marque nomMarque){
this.prix=prix;
this.nom=nom;
this.nomMarque=nomMarque;
}
Vehicule(){
prix=0d;
nom="";
nomMarque=null;
options=null;
moteur=null;
}
public String toString() {
String str="Je suis une voiture "+ nom + " de la marque "+ nomMarque;
return str;
}
public void addOption(Option opt) {
options.add(opt);
}
public Marque getMarque() {
return nomMarque;
}
public List<Option> getOptions() {
return options;
}
public double getPrix() {
return prix;
}
public void setMoteur(Moteur mot) {
this.moteur=mot;
}
}
Class Lagouna
public class Lagouna extends Vehicule {
Lagouna(){
super();
}
}
Class GPS
public class GPS implements Option{
static double prix=113.5d;
public double getPrix() {
return prix;
}
}
Class Option
public interface Option {
public double getPrix();
}