Good evening I am working with a JTable, taking code from a previous creation for this one. What I would like to do is dynamically update the table. How I am doing it is I am storing information in a linkedlist and then going through the list pulling the information to fill the table. The problem is that I am getting a null pointer and I am confused as to why. The start of my stack trace is:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at main$CoverSelection.actionPerformed(main.java:225)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
Obviously it is happening at line 225 which is reads as:
CartTable.setModel(new DefaultTableModel(data,ColumnNames));
Looking through past questions that are similar I keep seeing people mentioning to make sure that everything is initialized, which I have. The full code:
data = new Object[1][4];
//CartTable.setModel(new DefaultTableModel(data,ColumnNames));
while(abc<OrderList.size())
{
System.out.println(OrderList.get(abc).getIsbn());
System.out.println(OrderList.get(abc).GetTitle());
System.out.println(OrderList.get(abc).getQuantity());
System.out.println(Double.toString(OrderList.get(abc).getPrice()));
//data = new Object[1][4];
//CartTable.setModel(new DefaultTableModel(data,ColumnNames));
if (data== null)
{
data = new Object[1][4];
data[0][0] = OrderList.get(abc).getIsbn();
data[0][1] = OrderList.get(abc).GetTitle();
data[0][2] = OrderList.get(abc).getQuantity();
data[0][3] = Double.toString(OrderList.get(abc).getPrice());
CartTable.setModel(new DefaultTableModel(data,ColumnNames));
}
else
{
System.out.println("In the else part");
Object [][] temp = new Object[data.length+1][4];
for (int rowCt =0; rowCt < data.length; rowCt++)
{
for (int colCt=0; colCt< 4; colCt++)
{
temp[rowCt][colCt] = data[rowCt][colCt];
}
}
temp[data.length][0] = OrderList.get(abc).getIsbn();
temp[data.length][1] = OrderList.get(abc).GetTitle();
temp[data.length][2] = OrderList.get(abc).getQuantity();
temp[data.length][3] = OrderList.get(abc).getPrice();
data = temp;
System.out.println("data length in else: "+data.length);
CartTable.setModel(new DefaultTableModel(data,ColumnNames));//line 225
}
The code works and will cycle through the first if statement were the data is null, but then when it goes through it again to get another row through the else statement is when it gives me the error. My initializers which are:
private Object[][] data = null;
private JTable CartTable;
String[] ColumnNames=new String[4];
Any help or suggestions? I think I have looked and covered all my bases, and just cant see it.