How to get data from a JTable?

Viewed 51970

OK, I want to get all the data from the first column of a JTable. I though the best way would be pulling it in to a ArrayList, so I made one. I also made an instance of a TableModel:

static DefaultTableModel model = new javax.swing.table.DefaultTableModel(); 
f.data.setModel(model); //f.data is the JTable

public static final void CalculateTotal(){
    ArrayList<String> numdata = new ArrayList<String>();

  for(int count = 1; count <= model.getRowCount(); count++){
      numdata.add(model.getValueAt(count, 1).toString());

  }
  System.out.println(numdata); 
}

This gives me a NullPointerException (cue screams). What am i doing wrong?

4 Answers
       DefaultTableModel model = (DefaultTableModel) table.getModel();

        for (int i = 0; model.getRowCount() > i; i++) {
            final String col1 = (String) model.getValueAt(i, 0);
            final String col2 = (String) model.getValueAt(i, 1);

            System.out.println("Row: " + i + " Col 0: " + col1);
            System.out.println("Row: " + i + " Col 1: " + col2);
        }
Related