I get an error Exception in thread "main" java.lang.NullPointerException

0

I get the following error in console:

Puede entrar un coche
Coche devuelto
Puede entrar un coche
Coche devuelto
Puede entrar un coche
  

Exception in thread "main" java.lang.NullPointerException         at Garaje.aceptarCoche (Garaje.java:27)         at Activity2.main (Activity2.java:21)

in the following classes:

Engine

public class Motor {

//Atributos
private int Litrosdeaceite;
private int CV;

//Constructor
public Motor(int CV) {
    this.Litrosdeaceite = 0;
    this.CV = CV;
}

//Getter
public int getLitrosdeaceite() {
    return Litrosdeaceite;
}

public int getCV() {
    return CV;
}

//Setter
public void setLitrosdeaceite(int litrosdeaceite) {
    Litrosdeaceite = litrosdeaceite;
}

}

Car

public class Coche {

//Atributos
private Motor motor;
public String Marca;
public String Modelo;
public double Averias;

//Constructor
public Coche(String marca, String modelo) {
    this.Marca = marca;
    this.Modelo = modelo;
}

//Getter
public Motor getMotor() {
    return motor;
}

public String getMarca() {
    return Marca;
}

public String getModelo() {
    return Modelo;
}

public double getAverias() {
    return Averias;
}

//Método acumular avería
public void acumularAveria(double valorAveria) {
    this.Averias += valorAveria;
}

}

Garage

public class Garaje {

//Atributos
public Coche coche;
public String AveriaAsociada;
public int NumeroCoches;

//Método1
public void aceptarCoche(Coche coche, String AveriaAsociada) {
    this.coche = coche;
    this.AveriaAsociada = AveriaAsociada;

//Atender un coche a cada momento
if (NumeroCoches == 0) {
    System.out.println("Puede entrar un coche");
} else {
    System.out.println("Garaje ocupado");
}

//Acumular averias
this.coche.acumularAveria(Math.random()*100);
this.NumeroCoches = 1;

//Aumentar en 10 el aceite del motor
if (AveriaAsociada.equals("Aceite")) {
    int Litrosdeaceite = coche.getMotor().getLitrosdeaceite();
    coche.getMotor().setLitrosdeaceite(Litrosdeaceite + 10);
}
}

//Método2
public void devolverCoche() {
    this.NumeroCoches = 0;
    System.out.println("Coche devuelto");
}

 } 

and in this:

public class Actividad2 {

/**
 * @param args
 */
public static void main(String[] args) {

    Garaje garaje = new Garaje();

    Coche coche1 = new Coche("Marca", "Modelo");
    Coche coche2 = new Coche("Marca", "Modelo");

    garaje.aceptarCoche(coche1, "Averia1");
    garaje.devolverCoche();

    garaje.aceptarCoche(coche2, "Averia2");
    garaje.devolverCoche();

    garaje.aceptarCoche(coche1, "Aceite");
    garaje.devolverCoche();

    garaje.aceptarCoche(coche2, "Aceite");
    garaje.devolverCoche();

    System.out.println("Informacion coche: " + coche1.getMarca() + coche1.getModelo() + coche1.getAverias());
    System.out.println("Informacion coche: " + coche2.getMarca() + coche2.getModelo() + coche2.getAverias());




}

}

What should I change or am I wrong?

    
asked by DrWho 24.10.2016 в 10:52
source

2 answers

3

The Motor attribute of your class coche never assign a value to it, so when you make coche.getMotor() return Null , the simplest way to solve the error is assigning a value to that attribute, using the builder (Maybe not the best but it works)

public Coche(String marca, String modelo,int  cv) {
  this.Marca = marca;
  this.Modelo = modelo;
  /* LLamamos al constructor de Motor y le pasamos el parametro cv*/
  this.motor= new Motor(cv);
}

and in your Actividad2

Coche coche1 = new Coche("Marca", "Modelo",12);
Coche coche2 = new Coche("Marca", "Modelo",14);
    
answered by 24.10.2016 / 11:26
source
0

Your problem is that you never instantiate the Motor object. Put it in the constructor of Car for example:

public Coche(String marca, String modelo, int CV) {
  this.Marca = marca;
  this.Modelo = modelo;
  this.motor = new Motor(CV);
}

And you call it in Activity:

Coche coche1 = new Coche("Marca", "Modelo", 90);
    
answered by 24.10.2016 в 11:17