-3

I got a lil issue right here, I'm supposed to make this method:

comprarEntrada: method that buys a ticket with the column and row assigned for the session from the hall itself. They purchase is registered in the object type Sesion.

I am told to make a cinema, and this is my code for this method:

public class Sala {

    private String pelicula;
    private ArrayList<Sesion> sesiones;
    private int columnas;
    private int filas;

    public Sala (String pelicula, String [] horaSesiones, int filas, int columnas){ 
        this.pelicula = pelicula;
        this.sesiones = new ArrayList <Sesion>(); 
        this.filas = filas;
        this.columnas = columnas;
    }

    public void comprarEntrada (int sesion, int fila, int columna){
        if(this.sesiones.get(sesion-1).getEstadoAsientos()[fila][columna]==0){
        this.sesiones.get(sesion-1).comprarEntrada(fila, columna);
    }
} 

The deal is that I'm not sure if I've done it correctly, I guess it isn't because it throws this exception:

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at cine.Sala.comprarEntrada(Sala.java:42)
    at predeterminado.TestVentaEntradasSala.main(TestVentaEntradasSala.java:38)

So please if you could kindly help me right here I'll appreciate it.

hadi.mansouri
  • 828
  • 11
  • 25
ITZAK
  • 1
  • 1
    Have you read the exception? What do you think "IndexOutOfBoundsException: Index: 0, Size: 0" could possibly mean? – JB Nizet Jun 25 '17 at 20:50

2 Answers2

0

IndexOutOfBoundsException is occurred when you access to an element of an array or collection which is not exist. In other word you access to an index which is greater than lenght-1 of your array or size-1 of your collection.

In your code, it is depends on how you use class 'Sala'. It seems that you does not set or does not add any element to ''sessiones' array list and try to get first element of it.

hadi.mansouri
  • 828
  • 11
  • 25
0

sesiones list has no value (Size: 0), so you cant get the first element (Index: 0)

Rodrigo João Bertotti
  • 5,179
  • 2
  • 23
  • 34