ArrayIndexOutOfBounds Java error handling an array

0

I do not know how to solve this error:

  

error: Exception in thread "main"   java.lang.ArrayIndexOutOfBoundsException: 0

It takes me to the next class, in the setFilas function, that I use to insert a row into a matrix.

package arrays;

public class Matriz 
{
    private int matriz[][];


    public Matriz(int numFil, int numCol)
    {
        numFil = 0;
        numCol = 0;
        matriz = new int[numFil][numCol];
    }

     public int[][] getMatriz(){
        return matriz;
    }

    public void setMariz(int [][] matriz)
    {
        this.matriz = matriz;
    }

    public int getNumFilas()
    {
        return matriz.length; //Devuelve el número de filas de la matriz
    }

    public int getNumColumnas()
    {
        return matriz[0].length; //Devuelve el número de columnas de la matriz
    }

    public int getValor(int fil, int col)
    {
        return matriz[fil][col];
    }

    public void setValor(int fil, int col, int valor)
    {
        matriz[fil][col] = valor;
    }

    public int[] getFila(int fil)
    {
        return matriz[fil];
    }

    **public void setFila(int fil, int[] array) 
    {
        matriz[fil] = array;
    }**

}

I call her here:

public Matriz vuelcaFichMatriz(Array adim)
    {
        Matriz matriz = new Matriz(adim.getValor(0), adim.getValor(1));
        int fila=0;
        String linea = leerLinea();
        while(linea != null)
        {
            ArrayString as = new ArrayString();
            as.arraySt = linea.split(" ");      //Pongo en cada posición del array de strings un número
            Array a = new Array(adim.getValor(1));

            for(int i=0; i < as.arraySt.length; i++)    //Recorro el array que me ha creado split
            {
                String cad = as.getValor(i);
                int n = Integer.parseInt(cad);  //Convierto cada String a int
                a.setValor(i, n);               //Lo meto en un array de enteros
            }

            matriz.setFila(fila, a.getArray());    
            fila++;
            linea = leerLinea();

        }
    
asked by Gloria 29.03.2018 в 17:00
source

1 answer

2

Greetings, Gloria.

The exception:

  

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

It tells us that the index you are trying to enter in the array is out of its dimensions, that is, that your array does not have an index 0 , and the truth, it makes a lot of sense if we see the constructor of your class Matriz :

public Matriz(int numFil, int numCol) {
    numFil = 0;
    numCol = 0;
    matriz = new int[numFil][numCol];
}

Why do you assign numFil = 0; and numCol = 0; ? Basically, you would be saying this:

matriz = new int[0][0];

This is an array without any space! And therefore, when you try to enter the first space of your array, the exception will skip over.

Really the two assignments that you make in the constructor are over, you should remove them and leave them like this:

public Matriz(int numFil, int numCol) {
    matriz = new int[numFil][numCol];
}
    
answered by 29.03.2018 в 18:25