Daniel Buckle
Daniel Buckle

Reputation: 628

Receiving an Exception in thread "AWT-EventQueue-0" java.lang.ArrayStoreException

Here is the exception I am receiving:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayStoreException

Here is my code:

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;
    }
}

Upvotes: 0

Views: 1579

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

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.

Upvotes: 1

Related Questions