Results 1 to 4 of 4
  1. #1
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    6

    Default Why do I get this IndexOutOfBounds Exception at JTable?

    Java Code:
    run:
    Loaded 16 orders.
    [COLOR="Red"](The GUI is used to create a customer and then search for him)[/COLOR]
    Found 1 customers:
    John Doe
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
            at javax.swing.JTable$1.getValueAt(JTable.java:668)
            at javax.swing.JTable.getValueAt(JTable.java:2686)
            at javax.swing.JTable.prepareRenderer(JTable.java:5703)
            at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2072)
            at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:1974)
            at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1770)
            at javax.swing.plaf.ComponentUI.update(ComponentUI.java:143)
            at javax.swing.JComponent.paintComponent(JComponent.java:752)
            at javax.swing.JComponent.paint(JComponent.java:1029)
            at javax.swing.JComponent.paintChildren(JComponent.java:862)

    Its strange because if n customers are found, I get ArrayIndexOutOfBoundsException: n.

    I originally thought there was no data in the table and thats why I get this error, but when 1 customer is found, there should be 1 customer in the table.

    Here is the method that attempts to set the table:

    Java Code:
        public void updateCustomerSearchTerm() {
            //some code to take JTextField text and use it to search and compile a List of customer objects
            ...
            if (foundCustomers.size() > 0) {
                System.out.println("Found " + foundCustomers.size() + " customers:");
                for (Customers.Customer c:foundCustomers) {
                    System.out.println(c.getForename() + " " + c.getSurname());
                }
            }
            customersTable.setModel(new CustomersTable(foundCustomers).getTableModel());
        }

    And here are the relevent methods from my CustomersTable class:

    Java Code:
        CustomersTable(java.util.List<Customers.Customer> customerList) {
            customers = customerList;
            table = transmitData();
        }
    
        public javax.swing.table.TableModel getTableModel() {
            return table.getModel();
        }
    
        private JTable transmitData() {
            columnNames = new String[] {"CID", "Registered", "Title", "Forename", "Surname", "DoorNo", "PostCode", "Points"};
            data = new Object[columnNames.length][customers.size()];
            int row = 0;
            for (Customers.Customer c:customers) {
                data[row++] = new Object[] {c.getID(),Date.getFormattedDate(c.getRegisterDate(),false),c.getTitle(),c.getForename(),c.getSurname(),c.getDoorNumber(),c.getPostCode(),c.getPoints()};
            }
            return (new JTable(data, columnNames));
        }

    I even tried putting a try-catch(AIOOBE ex) block around customersTable.setModel(...) but the same thing happened! Now I don't know where else to put it because the exception is being thrown from a Java library.

    :confused:

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    6

    Default

    Uhh nevermind, I had the Object[rows][cols] sizes the wrong way around. Sorry

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,299
    Blog Entries
    7
    Rep Power
    24

    Default

    Change this:

    Java Code:
     data = new Object[columnNames.length][customers.size()];
    to this:

    Java Code:
     data = new Object[customers.size()][];
    because every row in the table corrresponds with a row in your array. Inside the body of the loop you allocate single rows and set them in your data array.

    kind regarfds,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    6

Similar Threads

  1. Java IndexOutOfBounds Exception: Index 0, Size 0
    By Maricica in forum Threads and Synchronization
    Replies: 7
    Last Post: 03-29-2011, 01:01 PM
  2. Exception in JTable
    By justbeller in forum AWT / Swing
    Replies: 2
    Last Post: 03-16-2011, 12:17 PM
  3. need help with IndexOutOfBounds error
    By ShinTec in forum Advanced Java
    Replies: 3
    Last Post: 04-29-2010, 09:21 AM
  4. ArrayList IndexOutOfBounds... error
    By Arius in forum New To Java
    Replies: 7
    Last Post: 01-17-2010, 11:15 PM
  5. Confusing IndexOutOfBounds Error?
    By tibbyuk in forum New To Java
    Replies: 2
    Last Post: 08-08-2008, 11:42 PM

Posting Permissions

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