I have to create a GUI which uses a table that takes data from an XML and then is able to sort the colums by clicking them ... so far so good except for the sorting part which gives me an NullPointerException (my favorite) and I can't figure it out since everything worked out fine until I put this line of code in table.setAutoCreateRowSorter(true);
Any ideas as to why it doesn't work?
JTable table;
public Table() {
ArrayList<Person> personList = XML.getPersonList();
JPanel mainPanel = new JPanel();
JPanel upperPanel = new JPanel();
JPanel lowerPanel = new JPanel();
JButton createPersonButton = new JButton("Create");
String[] collumnNames = { "Name", "Age", "Email" };
String[][] data;
data = new String[50][3];
createPersonButton.addActionListener(new createListener());
setLayout(new GridLayout(1, 2));
for (int i = 0; i < personList.size(); i++) {
data[i][0] = personList.get(i).getEmail();
data[i][1] = personList.get(i).getFullName();
data[i][2] = personList.get(i).getAge();
}
table.setAutoCreateRowSorter(true);
table = new JTable(data, collumnNames);
table.setPreferredScrollableViewportSize(new Dimension(430, 300));
table.setFillsViewportHeight(true);
JScrollPane scroll = new JScrollPane(table);
table.setFillsViewportHeight(true);
lowerPanel.add(createPersonButton);
upperPanel.add(scroll);
mainPanel.add(upperPanel);
mainPanel.add(lowerPanel);
setContentPane(mainPanel);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(650, 400);
setVisible(true);
}
}