Link to home
Start Free TrialLog in
Avatar of mbunkows
mbunkows

asked on

Null Pointer in JTable Model

I get a Null Pointer Exception when I try to run the following code.  The String[][] is not null when the constructor for the model is formed.
getRowCount() is called by the table the data is then Null
The output and a small section of the stack trace is below:
Data Values: 1-1
Data Values: 2-1
Data Values: 3-1
LEN: 3
java.lang.NullPointerException:
      at TableTry$GraphTableModel.getRowCount(TableTry.java:130)
      at javax.swing.JTable.getRowCount(JTable.java:1360)

//TableTry.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
import java.io.*;

public class TableTry extends JPanel {
      String heading[];
      Button cancel;
      String newline;
      Vector vect;
     int marker=0;

      public TableTry(Vector vect) {

            this.vect= vect;
            this.setLayout(new BorderLayout(20,20));
            this.setBackground(Color.lightGray);
            JPanel head= new JPanel();
            head.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
          head.setBackground(Color.lightGray);
          this.add("North",head);
            head.setFont(new Font("SansSerif",Font.BOLD,20));
            heading=(String[]) vect.elementAt(0);
            head.add(new JLabel(heading[0]));

            JPanel body= new JPanel();
            body.setLayout(new FlowLayout(FlowLayout.CENTER));
         
            GraphTableModel gtm= new GraphTableModel();
            JTable table= new JTable(gtm);
          initColumnSizes(table, gtm);                            //initializes the column sizes

          table.setPreferredScrollableViewportSize(new Dimension(500,100));
         
          JScrollPane scrollPane = new JScrollPane(table);
          body.add(scrollPane);
            this.add(BorderLayout.CENTER,body);            
      }
     
     private void initColumnSizes(JTable table, GraphTableModel model) {

        TableColumn column = null;
        Component comp = null;
        int headerWidth = 0;
        int cellWidth = 0;
        Object[] longValues = model.longValues;

        for (int i = 0; i < table.getColumnCount(); i++) {
            column = table.getColumnModel().getColumn(i);

            comp = column.getHeaderRenderer().getTableCellRendererComponent(null, column.getHeaderValue(),false, false, 0, 0);
            headerWidth = comp.getPreferredSize().width;

            comp = table.getDefaultRenderer(model.getColumnClass(i)).getTableCellRendererComponent(table, longValues[i],false, false, 0, i);
            cellWidth = comp.getPreferredSize().width;
            column.setPreferredWidth(Math.max(headerWidth, cellWidth));
        }
    }
     
     /**
     Main method used for testing purposes only
     Creates a Frame and uses sample data from a file
     in the current directory named "1099wmac.dat"
     */
     
     public static void main(String s[])  {
         
          //TEMP
          Vector vect= new Vector();
          String[] temp1= {"Heading"};
          String[] temp2= {"first","second","third","fourth","fifth","sixth","seventh","eighth"};
          String[] temp3= {"1-1","1-2","1-3","1-4","1-5","1-6","1-7","1-8"};
          String[] temp4= {"2-1","2-2","2-3","2-4","2-5","2-6","2-7","2-8"};
          String[] temp5= {"3-1","3-2","3-3","3-4","3-5","3-6","3-7","3-8"};
          vect.addElement(temp1);
          vect.addElement(temp2);
          vect.addElement(temp3);
          vect.addElement(temp4);
          vect.addElement(temp5);
         
          JFrame f= new JFrame("Test");
          //FileData vals= new FileData();
          //try  {
          //     f.getContentPane().add(new TableTry(vals.getGrafData("1099wmac.dat")));
          //}
          //catch(IOException e)  {
          //     System.out.println("IO Problem");
          //}
          f.getContentPane().add(new TableTry(vect));
         
          f.addWindowListener(new WindowAdapter()  {
               public void windowClosing(WindowEvent e)  {
                    System.exit(0);
               }
          });
          f.setSize(800,600);
          f.setVisible(true);
     }

     
     /**Table Model class -- defines the table data and column info
     */
     
     class GraphTableModel extends AbstractTableModel {
          String[] columnNames;
          String[][] data;
          String[] longValues= {"first","second","third","fourth","fifth","sixth","seventh","eighth"};
         
          GraphTableModel()  {
         
               columnNames = (String[])vect.elementAt(1);
               String[][] data= new String[vect.size()-2][];
               for (int i=2;i<vect.size();i++)  {
                    if (vect.elementAt(i) == null) break;
                    data[i-2]= (String[])vect.elementAt(i);
                    System.out.println("Data Values: " + data[i-2][0]);
               }
               System.out.println("LEN: " + data.length);
               //need to actually calculate the long values but for now use test data
          }
          public int getColumnCount() {
               return columnNames.length;
          }
          public int getRowCount() {
               //System.out.println("In getRowCount(): " + data.length);   //data is NULL -- why??
               return data.length;
          }
          public String getColumnName(int col) {
               return columnNames[col];
          }
          public Object getValueAt(int row, int col) {
               return data[row][col];
          }
     }
}
ASKER CERTIFIED SOLUTION
Avatar of ssbera
ssbera

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mbunkows
mbunkows

ASKER

Thats the SECOND time Ive let that stump me.
I'll learn I guess
Thanks!