Results 1 to 7 of 7
  1. #1
    Allgorythm is offline Member
    Join Date
    Dec 2009
    Posts
    20
    Rep Power
    0

    Default array Index out of Bounds exception== 0

    Hi I cant figure why does the program telling me an ArrayIndexOutOfBounds when adding a row in a Table?

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.*;
    public class DataBaseSample {
    
         private JTable mainTable;
         private JScrollPane sp ;
         private JViewport jv;
         private JTable rowHeaderTableColumn;
         private JPanel panel;
         private JButton rowRemover;
         private JFrame frame;
         private TableModel tableModel;
         private DefaultTableModel mainTableModel;
         private Integer[] rowHeaderData;
    
         public DataBaseSample() {
    
             frame = new JFrame();
             rowRemover = new JButton("Add Row");
             panel = new JPanel();
             tableModel = new AbstractTableModel() {
    
                 String[] header = {"Rows"};
    
                 public int getColumnCount() {
    
                     return 1;
                 }
    
                 public int getRowCount() {
    
                     return mainTable.getRowCount();
                 }
    
                @Override
                 public String getColumnName(int col) {
    
                     // this will set the row headers column name
                     return header[0];
                 }
    
                 public Object getValueAt(int row, int col) {
    
                     // this will set the row headers row data
                     return rowHeaderData[col] + row;
                 }
             };
    
             TableColumnModel rowHeaderModel = new DefaultTableColumnModel() {
    
                 boolean first = true;
    
                @Override
                 public void addColumn(TableColumn tableColumn) {
    
                     if (first) {
    
                         tableColumn.setMaxWidth(tableColumn.getPreferredWidth());
    
                         super.addColumn(tableColumn);
                         tableColumn.setMaxWidth(100);
                         first = false;
                     }
                 }
             };
    
             Object[][] rows = new Object[0][3];
             String[] cols = {"Column 1", "Column 2", "Column 3"};
             
    
             mainTableModel = new DefaultTableModel(rows, cols);
             mainTable = new JTable(mainTableModel);
             rowHeaderTableColumn = new JTable(tableModel, rowHeaderModel);
             rowHeaderTableColumn.createDefaultColumnsFromModel();
             rowHeaderTableColumn.setBackground(Color.lightGray);
             rowHeaderTableColumn.setColumnSelectionAllowed(false);
             rowHeaderTableColumn.setCellSelectionEnabled(false);
    
             jv = new JViewport();
             jv.setView(rowHeaderTableColumn);
             jv.setPreferredSize(rowHeaderTableColumn.getMaximumSize());
    
             mainTable.setSelectionModel(rowHeaderTableColumn.getSelectionModel());
             mainTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
             panel.setLayout(new BorderLayout());
    
             rowHeaderData = new Integer[mainTable.getRowCount()];
             for (int count = 0; count <= mainTable.getRowCount() - 1; count++) {
    
                 rowHeaderData[count] = count + 1;
             }
    
             sp = new JScrollPane(mainTable);
             sp.setRowHeader(jv);
             sp.setCorner(ScrollPaneConstants.UPPER_LEFT_CORNER, rowHeaderTableColumn.getTableHeader());
    
             rowRemover.addActionListener(new RowRemoverButtonActionListener());
    
             panel.add(rowRemover, BorderLayout.NORTH);
             panel.add(sp, BorderLayout.SOUTH);
    
             frame.getContentPane().add(panel);
    
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.setSize(500, 500);
             frame.setLocationRelativeTo(null);
             frame.setVisible(true);
         }
    
         private class RowRemoverButtonActionListener implements ActionListener {
    
             public void actionPerformed(ActionEvent e) {
    
                 // set to zero to avoid array index out of bounds -1
                 mainTableModel.addRow(new Object[] {null,null,null});
    
                 sp.setRowHeader(jv);
             }
         }
    
         public static void main(String args[]) {
             
             new DataBaseSample();
     
         }
    }

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    I just rushed through the code, so I'm not a 100% on this, but I'm pretty sure this is the problem:
    Java Code:
    Object[][] rows = new Object[0][3];
    Why are you trying to create a table with 0 elements? int[] array = new int[1]; will have only one element at index 0, is this perhaps what you wanted? Since multidimensional arrays are formed like this (this is what java does in the background, I'm not saying you should do this):
    Java Code:
    int[][] array = new int[1][3];
    //the equivalent
    int[] array2 = new int[1];
    array2[0] = new int[3];

  3. #3
    jassi is offline Member
    Join Date
    Feb 2010
    Location
    Dehradun
    Posts
    5
    Rep Power
    0

    Post Resolved

    The declaration of your rows is wrong. You are declaring it with 0 rows.

    Object[][] rows = new Object[0][3];

    Solution:

    Declare your object as

    Object[][] rows = new Object[1][3];

  4. #4
    Allgorythm is offline Member
    Join Date
    Dec 2009
    Posts
    20
    Rep Power
    0

    Default

    Java Code:
    Why are you trying to create a table with 0 elements?
    i was just trying to put my program or table in a situation like this. what if the default number of rows is zero? just in case and for
    the compiler is pointing on this part >
    Java Code:
      public Object getValueAt(int row, int col) {
    
                     // this will set the row headers row data
                     return rowHeaderData[col] + row;
                 }
    i tried to make an if-else statement that if rowHeaderData.length == 0, do something, or col == 0 then do something and row == 0 , then do something..

    i tried to make them 1 if their values are 0, but still the compier throws me an ArrayOutOfBounds
    Last edited by Allgorythm; 02-11-2010 at 01:41 PM.

  5. #5
    jassi is offline Member
    Join Date
    Feb 2010
    Location
    Dehradun
    Posts
    5
    Rep Power
    0

    Default

    Then you check it first that it is zero.

    If it is zero then add a row from your side only.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Perhaps you want your rowHeaderData to not be an array but instead an ArrayList, and then when you add a row, add to this list as well.

    edit: Ignore the above. I've not used row headers before, but perhaps something as simple as changing the row header's table model's getValueAt(..) to
    Java Code:
             public Object getValueAt(int row, int col) {
                return "Row " + row;
             }
          };
    but again, this is "shooting from the hip". Let's see what the experts say...
    Last edited by Fubarable; 02-11-2010 at 03:28 PM.

  7. #7
    Allgorythm is offline Member
    Join Date
    Dec 2009
    Posts
    20
    Rep Power
    0

    Smile

    :eek:
    edit: Ignore the above. I've not used row headers before, but perhaps something as simple as changing the row header's table model's getValueAt(..) to
    Code:
    public Object getValueAt(int row, int col) {
    return "Row " + row;
    }
    };
    but again, this is "shooting from the hip". Let's see what the experts say...
    are you not sure of this? but it helped me! it SOLVED the problem!
    i never thought of that, instead of
    Java Code:
    return rowHeaderData[col] + row;
    -relying to the data counts of the main table ,your code can generate my rowheadertable its own data counts.. wow thanks!

    i just did this one. >
    Java Code:
            
         public Object getValueAt(int row, int col) {
    
                     
                     return 1 + row;
                 }
    i just only need a number that will represents the count for each row, i hope i wont encounter any future problems. thanks
    !

Similar Threads

  1. [SOLVED] Array index out of bounds exception
    By sruthi_2009 in forum New To Java
    Replies: 6
    Last Post: 03-23-2014, 10:16 AM
  2. Array Index Out Of Bounds and Problem in Assigning Values
    By chronoz1300 in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 07:14 PM
  3. Array Index Out of Bounds Exception
    By kool001 in forum New To Java
    Replies: 1
    Last Post: 12-03-2009, 07:42 AM
  4. Array out of bounds exception 20.
    By dropt in forum New To Java
    Replies: 4
    Last Post: 09-21-2009, 10:32 PM
  5. Array Index out of bounds
    By leapinlizard in forum New To Java
    Replies: 5
    Last Post: 04-29-2009, 05:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •