0

这是我收到的例外情况:

线程“AWT-EventQueue-0”中的异常 java.lang.ArrayStoreException

这是我的代码:

public class LeagueTable extends JTable {

    public static final Dimension INITIAL_SIZE = new Dimension(500, 300);
    public final String[] columnNames;
    DefaultTableModel model;
    JTable table;

    public LeagueTable(){

        DatabaseConnector listOfTeams = new DatabaseConnector();
        columnNames = new String[]{"Teams", "Goal Difference", "Points", "Verdict"};
        Object[][] data = listOfTeams.teamResults.toArray(new Object[listOfTeams.teamResults.size()][]);
        model = new DefaultTableModel(data, columnNames);
        table = new JTable(model){@Override
                                public boolean isCellEditable(int row, int column) {

            return false;
        }};

        JScrollPane pane = new JScrollPane(table);
        setVisible(true);
        setSize(INITIAL_SIZE);
        setLayout(new FlowLayout());

        add(pane);
    }

    public String[] getStringArray() {
        return columnNames;
    }
}
4

1 回答 1

1

So I was right, the line:

Object[][] data = listOfTeams.teamResults.toArray(new 
         Object[listOfTeams.teamResults.size()][]);

Is causing your problem, and this makes sense since you are not in fact creating a 2 D array on this line of code, but only a one dimensional array. You need to either create row objects that are arrays of objects for this to work or not use the DefaultTableModel. If you want to stick with DefaultTableModel, then you will likely need to use a for loop to fill your array prior to trying to use it in this constructor.

于 2013-09-30T22:06:52.880 回答