Search code examples
javaswingjtableindexoutofboundsexception

JTable throws an ArrayIndexOutOfBoundsException


This is an UI that shows up random values with a JTable. It works fine all the math procediments but when I apply them to the JTable, it throws me an ArrayIndexOutOfBoundsException. ¿Could yout tell me what's going wrong and how to solve it? Finally, I have to thank you for spending time in search of this malfunctioning.

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class RandomVals extends JFrame {

    JTable a;
    JScrollPane b;

    public RandomVals () {
      String [] header = new String[15];                 //Fulfills 2D array
        for (int i = 0; i < header.length; i++) {        //with random numbers
            header[i]=String.valueOf(i);
        }

      String [][] rows = new String[15][8];
        for (int i = 0; i < rows.length; i++) {
          System.out.println();
            for (int j = 0; j < 8; j++) {
                rows [i][j] = "Hi";
                double foo = (Math.random()*2)+8;
                rows [i][j] = String.format("%.2f",foo);
                  System.out.print(rows[i][j]+" ");
            }
        }
        /////////////  All above works but then an exception occurs //////////////

        a = new JTable(rows, header);  //Creating JTable
          b = new JScrollPane(a);
          b.setBounds(10,10,500,500);
        add(b);
    }

    public static void main(String[] args) {  //JFrame build up
        RandomVals e = new RandomVals();
          e.setSize(520,520);
          e.setLocationRelativeTo(null);
          e.setVisible(true);
          e.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

Solution

  • You're creating a header with 15 columns...

    String[] header = new String[15];                 //Fulfills 2D array
    for (int i = 0; i < header.length; i++) {        //with random numbers
        header[i] = String.valueOf(i);
    }
    

    But then creating row data with 15 rows and 8 columns

    String[][] rows = new String[15][8];
    for (int i = 0; i < rows.length; i++) {
        System.out.println();
        for (int j = 0; j < 8; j++) {
            if (false) {
                rows[i][j] = "Hi";
            } else {
                double foo = (Math.random() * 2) + 8;
    
                rows[i][j] = String.format("%.2f", foo);
    
                System.out.print(rows[i][j] + " ");
            }
        }
    }
    

    Changing the above to something more like...

    String[][] rows = new String[8][15];
    for (int i = 0; i < rows.length; i++) {
        System.out.println();
        for (int j = 0; j < 15; j++) {
            if (false) {
                rows[i][j] = "Hi";
            } else {
                double foo = (Math.random() * 2) + 8;
    
                rows[i][j] = String.format("%.2f", foo);
    
                System.out.print(rows[i][j] + " ");
            }
        }
    }
    

    seems to work for me