inconveniences with the method darPasajeroMasSillasAsignadas ()

0

Good morning classmates I have some problems with this exercise, I do not have much time in this programming, this is the statement:

Statement Create the method given by Passenger Assigned Chairs, which returns the passenger who has more assigned seats on the plane. If there are several with the same number of chairs assigned, the first one is returned.

Note: I have a method that gives me the number of seats assigned to a passenger named darNumSillasAsignadasPasajero (); It is not in the class diagram since it is part of the exercise but I think it helps me to solve this.

This is my code:

/**
 * Retorna el pasajero que tiene más sillas asignadas en el avión
 * @return - Pasajero con más sillas asignadas en el avión
 */
public Pasajero darPasajeroMasSillasAsignadas()
{
   Pasajero mayorSillasEjecutivas = null;
   Pasajero mayorSillasEconomicas = null;
   int ejecutivas = 0;
   int economicas = 0;
   for(int i = 0; i < SILLAS_EJECUTIVAS; i++){
       if(sillasEjecutivas[i].sillaAsignada()){
           for(int j = 0; j < SILLAS_EJECUTIVAS; j++){
               if(sillasEjecutivas[j].sillaAsignada()){
                   if (sillasEjecutivas[i].darNumero() != sillasEjecutivas[j].darNumero()){
                       if(darNumSillasAsignadasPasajero(sillasEjecutivas[i].darPasajero()) >= darNumSillasAsignadasPasajero(sillasEjecutivas[j].darPasajero())){
                          mayorSillasEjecutivas = sillasEjecutivas[i].darPasajero();
                          ejecutivas = darNumSillasAsignadasPasajero(sillasEjecutivas[i].darPasajero());
                       }

                    }    

               }
           }
       }

    }
    for(int i = 0; i < SILLAS_ECONOMICAS; i++){
       if(sillasEconomicas[i].sillaAsignada()){
           for(int j = 0; j < SILLAS_ECONOMICAS; j++){
               if(sillasEconomicas[j].sillaAsignada()){
                   if(sillasEconomicas[i].darNumero() != sillasEconomicas[j].darNumero()){
                      if(darNumSillasAsignadasPasajero(sillasEconomicas[i].darPasajero()) >= darNumSillasAsignadasPasajero(sillasEconomicas[j].darPasajero())){
                         mayorSillasEjecutivas = sillasEjecutivas[i].darPasajero();
                         economicas = darNumSillasAsignadasPasajero(sillasEconomicas[i].darPasajero());
                      }
                   }  
               }
           }
       }

    }
    if(ejecutivas >= economicas ){
        return mayorSillasEjecutivas;
    }
    else{
        return mayorSillasEconomicas;
    }
}      

The exercise compiles me but throws me the following error:

java.lang.ArrayIndexOutOfBoundsException: 10 java.lang.NullPointerException

this is the class diagram:

link

thanks in advance ...

    
asked by Jhon James Hernandez 06.02.2018 в 01:21
source

1 answer

2
  

ArrayIndexOutOfBoundsException: 8 java.lang.NullPointerException: means that you are trying to access an item in your array that is out of bounds.

For example:

int [] array = new int[10]; // este array tiene 10 posiciones de la 0 a la 9
array[10]; // trato de acceder a la posición 10 (siendo la 9 la última), entonces esto es un error de compilación ArrayIndexOutOfBoundsException

As I see there is something strange in your code that could give you the error that I mentioned and it is on line 31 (In the for of SILLAS_ECONOMICAS), as I see you are iterating based on the array of sillasEconómicas and in this case these trying to access the array of sillasEjecutivas . Should not it be like this?:

for(int i = 0; i < SILLAS_ECONOMICAS; i++){
   if(sillasEconomicas[i].sillaAsignada()){
       for(int j = 0; j < SILLAS_ECONOMICAS; j++){
           if(sillasEconomicas[j].sillaAsignada()){
               if(sillasEconomicas[i].darNumero() != sillasEconomicas[j].darNumero()){
                  if(darNumSillasAsignadasPasajero(sillasEconomicas[i].darPasajero()) >= darNumSillasAsignadasPasajero(sillasEconomicas[j].darPasajero())){
                     mayorSillasEjecutivas = sillasEjecutivas[i].darPasajero();
                     economicas = darNumSillasAsignadasPasajero(sillasEconomicas[i].darPasajero());
                  }
               }  
           }
       }
   }

}

Instead of this line:

if(sillasEjecutivas[j].sillaAsignada())

Use this:

if(sillasEconomicas[j].sillaAsignada())

I hope it has helped you:)

    
answered by 06.02.2018 в 01:33