Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: ArrayIndexOutOfBoundsException when creating new JTable

  1. #1
    Junior Member
    Join Date
    Sep 2012
    My Mood
    Thanked 0 Times in 0 Posts

    Default ArrayIndexOutOfBoundsException when creating new JTable

    I apologise in advance for the length of this post but I have provided a rather in depth of the design and implementation of my program.


    I am currently doing a group (there are 2 of us) programming project for a third year computer science course at university. The goal of this program is to essentially use a spreadsheet program to represent XML file data where each XML file is a historical record.


    Each record(row) in the spreadsheet corresponds to a single XML file and the columns of the record correspond to the elements in the XML file. We deal with repeated elements (i.e. elements with the same tag) by setting the cell component to a button that, when clicked, opens up another spreadsheet which contains a list of all elements with repeated names (for the corresponding file). Child elements are dealt with in a similar manner whereby if an element ha child elements then the corresponding cell in the XML file contains a button which, when clicked, opens up a spreadsheet containing all the child elements of that element.


    The implementation of our system is written in Java. Our main class (name SpreadSheetGUI) extends JFrame to which we add a JTable (using the default table model). We have three different cell renderers: one for when a cell just has text, one for when a cell has a button and one for when we have text and a button in a cell. When a button is clicked to open up a new spreadsheet (for either child elements or repeated element names) we make a recursive call to our spreadsheet constructor which will then create our sub-spreadsheet. The renderers are added in the following order: if the cell corresponds to an element with a tag that is used more than once a button is added to the cell, otherwise if the cell corresponds to an element with child nodes we add both text and a button to the cell and if the cell just has text we add the text to that cell.

    The constructor for the GUI is as such
         * Parameterised constructor
         * @param dataVector - Vector of vectors of objects that represent the cell values
         * @param columnNames - The vector of objects that represent the column names
        public SpreadSheetGUI(Vector<Vector<LevelElementT>> dataVector, Vector<String> columnNames, boolean hasRepeatedColumns, boolean initialFrame)
            this.hasRepeatedColumns = hasRepeatedColumns;
            this.initialFrame = initialFrame;
            if (initialFrame)
                populateTable(dataVector, columnNames);
            else if (!hasRepeatedColumns)
                populateTable(dataVector, columnNames);
            //Get repeated column names and add to repeated column hashmap

    where the populateTable method initialises the table model. And as I stated earlier we have three different renderers and editors for our cells and two of the renderers have buttons in them that, when clicked, create a new spreadsheet (i.e. we make a call to our spreadsheet constructor), for example in our one cell editor has the following code :

    public Object getCellEditorValue() 
                  if (isPushed)
                        //will have the child elements of the current cell element
                    Vector<Vector<LevelElementT>> children = new Vector<Vector<LevelElementT>>();
                    children.add(new Vector<LevelElementT>());
                    List<Element> tempChildren = elements.get(row).get(column).getChildren();
                    for (Element child : tempChildren)
                        children.get(0).add(new LevelElementT(child));
                    //creates our subspreadsheet
                    new Thread(new SpreadSheetGUI(children, new Vector<String>(), false, false)).start();
                  isPushed = false;
                  return new String(bLabel);

    LevelElementT is just a class we made that extends Element (found in the JDOM2 package) that overrides the toString method.

    The Problem:

    As I mentioned before we have created a set of renderers to handle the adding of buttons to cells but it seems that when a "child" spreadsheet is created the renderers are trying to render cells that are out of bounds in accordance with the child spreadsheets number of rows and columns and throw an array index out of bounds exception.
    More specifically the errors are thrown in the following piece of code taken from the populateTable() method. I initialises the JTable with an instance of the defaultTableModel and sets up the methods to determine the renderer of each component:

    table = new JTable(tableModel)
                private static final long serialVersionUID = 1L;
                public TableCellRenderer getCellRenderer(int row, int column)
                    if (column == 0)
                        Class<? extends Object> cellClass = getValueAt(row, column).getClass();
                        return getDefaultRenderer(cellClass);
                    else if(repeatedColumns.containsKey(table.getColumnModel().getColumn(column).getIdentifier() + " " + elements.get(row).get(0).getText()))
                            return getDefaultRenderer(JButton.class);
                    else if(!elements.get(row).get(column).getChildren().isEmpty())
                            return getDefaultRenderer(JPanel.class);
                            return getDefaultRenderer(JTextArea.class);
                public TableCellEditor getCellEditor(int row, int column)
                    if (column == 0)
                        Class<? extends Object> cellClass = getValueAt(row, column).getClass();
                        return getDefaultEditor(cellClass);
                    else if(repeatedColumns.containsKey(table.getColumnModel().getColumn(column).getIdentifier() + " " + elements.get(row).get(0).getText()))
                            return getDefaultEditor(JButton.class);
                    else if(!elements.get(row).get(column).getChildren().isEmpty())
                            return getDefaultEditor(JPanel.class);
                            return getDefaultEditor(JTextArea.class);

    The errors are thrown at the if statements (depending on the button you clicked) and it is definitely not because of elements (a two dimensional vector of levelElements) or repeatedColumns(a hashtable of with a string as the key and a vector of elements as the value).

    I'm guessing the problem extends from the fact that we are making a recursive call to our spreadsheet constructor. A friend also suggested that this problem might be caused by the default table model, should I consider creating a custom table model?

    I have been wracking my brains with this one and I have been completely unsuccessful in finding any threads related to this problem. I have posted this problem on StackOverflow with little sucess,
    Last edited by KentHawkings; September 20th, 2012 at 12:16 PM. Reason: Formatting

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,372 Times in 2,343 Posts

    Default Re: ArrayIndexOutOfBoundsException when creating new JTable

    Can you post the full text of the error message that shows where the error happened and what the value of the index is?
    Also have you located the statement where the error happens and added some debug code to print out the value of the index and the size of the array involved in the error.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. ArrayIndexOutOfBoundsException issue (Beginner?)
    By cziiki in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 15th, 2012, 07:12 AM
  2. Help! ArrayIndexOutOfBoundsException
    By ray3 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: December 1st, 2011, 06:21 PM
  3. Creating simple Jtable
    By Dharmarajan in forum AWT / Java Swing
    Replies: 1
    Last Post: March 4th, 2011, 10:24 AM
  4. [SOLVED] ArrayIndexOutOfBoundsException
    By Elementality in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 18th, 2011, 07:39 PM
  5. ArrayIndexOutOfBoundsException
    By NightFire91 in forum Exceptions
    Replies: 1
    Last Post: October 31st, 2010, 07:06 AM

Tags for this Thread