-1

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Paul Robert Carlson
  • 169
  • 1
  • 2
  • 14
  • 1
    For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Dec 12 '12 at 01:49
  • 2
    The VM throws NullPointerException when you try to call a method or field on null. So is CartTable null? (Side note: fields and variables should be lowerCamelCase.) – ignis Dec 12 '12 at 01:52
  • 2
    `"I think I have looked and covered all my bases,..."` But why aren't you showing us where you initialized the only variable on the offending line that can cause this exception, the JTable itself? You seem to be missing the most important base! – Hovercraft Full Of Eels Dec 12 '12 at 02:22

2 Answers2

1

A) Java is not C/C++. There is no concept of 'automatically created objects for local variables' in Java. All objects has to be created explicitly. So, always

Object o = new Object();

if you don't want to get NPEx when doing o.something() later on.

And, here you have to:

private JTable CartTable = new JTable(/*Object[][]*/ rowData, /*Object[]*/ columnNames);

B) use OrderList.get(abc) only once, and store the result. Doing it over and over again is inefficient.

1

The problem with the line is,

CartTable.setModel(new DefaultTableModel(data,ColumnNames));

You are just declaring a JTable CartTable and you are not creating object for it any where. Before you set the model just create the table object like,

JTable CartTable = new JTable();

No need to set data and column names immediately you can later create a model with data and column names and then set this model to the table.

P.S: Please read the answer post @Vincent Ramdhanie for What is a Null Pointer Exception?. NPE occurring was explained very well.

Community
  • 1
  • 1
Amarnath
  • 8,736
  • 10
  • 54
  • 81