0

I've searched for my problem for hours, but none of the solutions I tried have solved it. I want to generate around 40 rows in a table with 6 columns for a project. However, when I run the code in a Swing GUI, the rows don't appear in the table. Each row is either a String or a Double, except for the last row, which is going to be a Panel. Here is the error message:

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

Here is the function with the problem:

public void generateRows() {
    setRowCount(0);
    int cont = 0;
    for (Object obj : productos) {
        addRow(obtenerAtributosProducto((ProductoComida) obj));
        cont++;
    }
    setRowCount(cont);
}
private Object[] obtenerAtributosProducto(ProductoComida producto) {
    /*
     *Los atributos se obtendrán en el siguiente orden:  "Id", "Nombre",
     *  "Precio", "Marca"
     * , "Calificación" 
     * */


    rowData[0] = producto.obtenerId().toString();
    rowData[1] = producto.obtenerNombre();
    rowData[2] = producto.obtenerPrecio();
    rowData[3] = producto.obtenerMarca();
    rowData[4] = producto.obtenerCalificacion();
    rowData[5] = "Here is a panel";

    return rowData;
}

This is the rest of the code:

public class ComprarComidaTableModel extends DefaultTableModel {
private static final String[] titles = {
        "Id", "Nombre",  "Precio", "Marca"
        , "Calificación", "Opciones"
};//La última fila es la fila del botón, la cantidad, y obtener más información del producto.

private Inventario inventario = new Inventario();;
private Object[] productos;
private String filtro;
private Object[] rowData = new Object[6];

public ComprarComidaTableModel() {
    super(0, 0);
    this.productos = inventario.obtenerCatComida().obtenerProductos();
    generateColumns(); 
    generateRows(); //This method is the problem
    this.filtro = "Ninguno";

}

I know that I don't need to do this for a single table, but I have to do almost 3 tables like this and I think this is the best way to simplify the code.

DaSe
  • 1
  • 3
  • 2
    Does this answer your question? [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – OH GOD SPIDERS Jun 05 '23 at 15:59
  • It's the _addRow_ method which is throwing the exception? – Reilas Jun 05 '23 at 18:19
  • Yes, It's addRow, I tried put a for to put a string per column, but I get the same error. for(int i = 0; i < titles.length; ++i) { rowData[i] = "Iteracion" + i; } – DaSe Jun 05 '23 at 18:49

0 Answers0