vendredi 14 mai 2021

Object Variable - The local variable may not have been initialized

I have searched for the problem in everywhere. People told me that I did define the variable but not initialized it. However, if the variable was integer I could just assign it to the 0 at the beginning, but it's an Object variable. What to assign it?

The error

public class main {
    public static void main(String[] args){
        Engine engine;
        MusicPlayer musicPlayer;
        Lights lights;
        LockDoors doors;
        
        
        System.out.println("FACADE PATTERN\n --------------------------");

        CarDriveFacade carDrive = new CarDriveFacade(engine, musicPlayer, lights, doors);
        carDrive.startCar();
        carDrive.stopCar();
    }

}

Facade Class:

public class CarDriveFacade {
    Engine engine;
    MusicPlayer musicPlayer;
    Lights lights;
    LockDoors doors;

    public CarDriveFacade(Engine engine, MusicPlayer musicPlayer, Lights lights, LockDoors doors){
        this.engine = engine;
        this.musicPlayer = musicPlayer;
        this.lights = lights;
        this.doors = doors;
    }
    public void startCar(){
        doors.close();
        engine.start();
        lights.on();
        musicPlayer.on();
    }

    public void stopCar(){
        musicPlayer.off();
        lights.off();
        engine.stop();
        doors.open();
        
    }
}

Subclass example:

public class LockDoors {
    LockDoors doors;

    LockDoors(LockDoors doors){
        this.doors=doors;
    }

    public void open(){
        System.out.println("Doors are open");
    }
    public void close(){
        System.out.println("Doors are closed");
    }
}


Aucun commentaire:

Enregistrer un commentaire