title: Java Swing JTable: cannot access the values in the table due to incorrect usage/creation of table model
body: <p>I am just beginning to program using Java Swing. I am not very familiar with each of the many Java Swing Classes, and have ran into a stumbling block regarding the creation of a Table.</p>
  
  <p>I am trying to figure out how to add a table to a Java Swing Program I am making. I have looked at docs.oracle.com to try and figure it out but I am getting an error when I try to access the values of the table. I will include my code to help show what I am currently trying to do and where the error is.</p>
  
  <p>This is my code for making the table and the table model (I think the problem is that my table model is not working correctly):</p>
  
  <pre><code>        /*Create the Table Entry*/
          columnNames = new String[listoffields.size()+1];
          columnNames[0] = "Record Number";
          for(int a = 1; a &lt; columnNames.length;a++){
              columnNames[a] = listoffields.get(a-1).getTitle();
          }
          int count = 1;
          data = new Object[numrecords][listoffields.size()+1];
          for(int a = 0; a &lt; numrecords;a++){
              for(int b = 0; b &lt; listoffields.size()+1;b++){
                  if(b == 0){
                      data[a][b] = count;
                      count++;
                  }
                  else{
                      data[a][b] = "";
                  }
              }
          }
  
          /* create the table */
          JTable table = new JTable(data,columnNames);
          table.setCellSelectionEnabled(true);
          table.setModel(new AbstractTableModel() {
              public String getColumnName(int col) {
                  return columnNames[col].toString();
              }
              public int getRowCount() { return data.length; }
              public int getColumnCount() { return columnNames.length; }
              public Object getValueAt(int row, int col) {
                  return data[row][col];
              }
              public boolean isCellEditable(int row, int col) {
                  return (col &gt;= 1);
              }
              public void setValueAt(Object value, int row, int col) {
                  data[row][col] = value;
                  fireTableCellUpdated(row, col);
              }
          });
          JScrollPane scrollPane = new JScrollPane(table);
          table.setFillsViewportHeight(true);
  
          /* addthe table to the JTable tableentry*/
          tabelentry.setLayout(new BoxLayout(ProjectFrame.tabelentry, BoxLayout.Y_AXIS));
          tabelentry.add(scrollPane);
          tabelentry.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  </code></pre>
  
  <p>After the table is displayed and I edit the blank values for each cell, I click a submit button that submits the cell. However, the following error is encountered: </p>
  
  <pre><code>Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 &gt;= 0
     at java.util.Vector.elementAt(Unknown Source)
     at javax.swing.table.DefaultTableModel.getValueAt(Unknown Source)
     at Client.GUIComponents.ProjectFrame$1.submit(NameOfFile.java:LineNumber)
  </code></pre>
  
  <p>The error is encountered when trying to submit as follows:</p>
  
  <pre><code>public void submit(){
     String recordvalues = ""; //contains a String representation of what I want to submit
     for(int a = 0; a &lt; numberoffields;a++){
        for(int b = 0; b &lt; numberofrecords;b++){
           if(a != 0){ //I want to skip the first column
              recordvalues = recordvalues  + (String) tabelentry.getModel().getValueAt(b, a) + ","; //The error is at this line in the code
           }
        }
     }
  }
  </code></pre>
  
  <p>I provided my code to give an example of the current problems I am encountering and what I have currently tried. To generalize my question, <strong>I am wondering how to correctly write a table model class so that I can access the values at each of the cells in the table.</strong> Any assistance would be helpful. Thanks a ton!</p>