The data of an Array is deleted -Java

0

I have to do a reservation management program in a hotel. It has different classes as Client and Room. From the View class, I send data to Controller to be forwarded to HotelDao. The data enters the reservation array within the HotelDao class. But when returning to the main window the data ceases to exist and at the moment of calling them they are not present and the null data error is thrown.

View class. Client selection method.

    //Aqui se piden los datos para Cliente
    public Cliente seleccionCliente() {
    Controller controller = new Controller();
    Cliente cliente = new Cliente();
    if(ocacional.isSelected()) {
        cliente = controller.crearCliente(
                campoNombre.getText(),
                campoApellido.getText(),
                campoDireccion.getText(),
                campoCiudad.getText(),
                campoNumeroTarjeta.getText());
    }else if(habitual.isSelected()) {
        int numeroDescuento = Integer.parseInt(campoDescuento.getText());
        cliente = controller.crearCliente(
                campoNombre.getText(),
                campoApellido.getText(),
                campoDireccion.getText(),
                campoCiudad.getText(),
                campoNumeroTarjeta.getText(),
                campoEmail.getText(),
                campoTelefono.getText(),
                numeroDescuento);
    }
    return cliente;
}

View class. Method to generate Reservation.

    //Aqui se envia la informacion a Controller
    public void generarReserva() {
    Controller controller = new Controller();
    controller.agregarReserva(controller.crearReserva(seleccionHabitacion(
            seleccionCliente())));        
    }

Controller class. Method add Reservation.

    //Reenvia los datos a la clase HotelDao
    public void agregarReserva(Reserva reserva) {
    hotelDao.agregarReserva(reserva);
}

Controller class. Method printEn Console

    //Aqui se elimina el error del nulo
    public void imprimirEnConsola(int seleccion) {
    seleccion = 0;
    if(seleccion < hotelDao.getReservas().length) {
    hotelDao.readReserva(seleccion);
    }
}

However, if I remove the null error, it will not show me that it fails. It still does not show data because it is as if it was reset.

HotelDao class. Method add Reservation.

    //Recibe y coloca los datos en el array
    public void agregarReserva(Reserva reserva) {
    reservas[contador] = new Reserva();
    reservas[contador] = reserva;
    contador++;
    System.out.println("Contador de reservas : " + contador);
}

HotelDao class. HotelDao Conductor.

    public class HotelDao {
        private Reserva[] reservas;
        private int contador;
        public HotelDao() {
            reservas = new Reserva[20];
            contador = 0;
        }
}

HotelDao class. Method readReserva. He prints them in the console because first I do that exercise to see if it works, but when I call it, the data is null.

    //Lee los datos almacenados.
    public Reserva readReserva(int seleccion) {
    System.out.println(
    "Numero de indice : " + seleccion+"\n" +
    "Minimo de horas : " + reservas[seleccion].getHabitacion().getHoraMinima()+"\n"+
    "Precio por hora : " + reservas[seleccion].getHabitacion().getPrecioPorHora()+"\n"+
    reservas[seleccion].getHabitacion().getCliente().getNombre()+"\n"+
    reservas[seleccion].getHabitacion().getCliente().getApellido()+"\n"+
    reservas[seleccion].getHabitacion().getCliente().getCiudad()+"\n"+
    reservas[seleccion].getHabitacion().getCliente().getDireccion()+"\n"+
    reservas[seleccion].getHabitacion().getCliente().getNumeroTarjeta()
    );
    return reservas[seleccion];
}

Finally this is the error that it generates. As I mentioned is a null in hotel because there is nothing and there must be something.

  

Exception in thread "main" java.lang.NullPointerException       at models.dao.HotelDao.readBook (HotelDao.java:73)       at controller.Controller.impressCompress (Controller.java:90)       at view.View.showBook (View.java:246)       at controller.Controller.reservaHotel (Controller.java:41)       at controller.Controller.run (Controller.java:27)       at runner.Runner.main (Runner.java:8)

Thanks for the help.

    
asked by Felipe Chaparro 13.09.2017 в 05:11
source

1 answer

0

The problem of resetting the class is because the program works like that. I solved it by returning vectors with the data and so if I could store and read them.

    
answered by 14.09.2017 в 03:29