3 Replies Latest reply on Aug 19, 2001 5:50 PM by 807582

    Arrrgh: crazy ArrayIndexOutOfBoundsException with JTable

    807582
      I am attaching 2 code snippets. Both code snippets have an identical area, except that one receives a hashtable that populates the JTable.

      Code Snippet 1 (Works fine)
      ---------------------------

      Common code ----
      public JPanel createTablePanel() {
                Vector reportHeader=new Vector();
      reportHeader.addElement("Parameter");
      reportHeader.addElement("Value");
      JPanel tablePanel = new JPanel();

      ParamTableModel paramModel = new ParamTableModel(reportHeader, 0);

      JTable paramTable = new JTable(paramModel);

      JScrollPane paramTableScrollPane = new JScrollPane(paramTable);

      paramTableScrollPane.setPreferredSize(new Dimension(474,150));

      paramTableScrollPane.setPreferredSize(new Dimension(487,150));

      tablePanel.add(paramTableScrollPane);


      -----code for snippet 1 only -----

      Vector dummyInfo=new Vector();
      dummyInfo.addElement("Parameter 1");
      dummyInfo.addElement("Value 2");
      for (int i=0;i<20;i++)
      paramModel.addRow(dummyInfo);

      THIS WORKS FINE
      ---------------------------------

      ----code for snippet 2 only ----
      (A hashtable (headerHash) is passed into the method and is used instead of the "dummy vector")

      Vector tempVector = new Vector();
      Enumeration enum = headerHash.elements();
      Enumeration keys = headerHash.keys();
      while(enum.hasMoreElements())     {
           String pkey = (String) keys.nextElement();
           System.out.println(pkey);
           tempVector.addElement(pkey);
           String pvalue = (String) enum.nextElement();
           System.out.println(pvalue);
           tempVector.addElement(pvalue);
           paramModel.addRow(tempVector);
           tempVector.removeAllElements();
      }

      -----------------------------------------
      Now all I am doing in code 2 is getting a hashtable out and putting its values in the table. Its not very different from code 1. It seems the table is being created without a problem - there is a problem only during "painting"

      Im getting the following exception:

      java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
      at java.util.Vector.elementAt(Vector.java:420)
      at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java
      :622)
      at javax.swing.JTable.getValueAt(JTable.java:1748)
      at javax.swing.JTable.prepareRenderer(JTable.java:3637)
      at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:1068)

      at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:990)

      at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:931)
      at javax.swing.plaf.ComponentUI.update(ComponentUI.java:136)
      at javax.swing.JComponent.paintComponent(JComponent.java:485)
      at javax.swing.JComponent.paint(JComponent.java:744)
      at javax.swing.JComponent.paintChildren(JComponent.java:591)
      at javax.swing.JComponent.paint(JComponent.java:753)
      at javax.swing.JViewport.paint(JViewport.java:697)
      at javax.swing.JComponent.paintChildren(JComponent.java:591)
      at javax.swing.JComponent.paint(JComponent.java:753)
      at javax.swing.JComponent.paintChildren(JComponent.java:591)
      at javax.swing.JComponent.paint(JComponent.java:753)
      at javax.swing.JComponent.paintChildren(JComponent.java:591)
      at javax.swing.JComponent.paint(JComponent.java:753)
      at javax.swing.JComponent.paintChildren(JComponent.java:591)
      at javax.swing.JComponent.paint(JComponent.java:753)
      at javax.swing.JComponent.paintChildren(JComponent.java:591)
      at javax.swing.JComponent.paint(JComponent.java:753)
      at javax.swing.JComponent.paintChildren(JComponent.java:591)
      at javax.swing.JComponent.paint(JComponent.java:753)
      at javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4594)

      at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4547)
      at javax.swing.JComponent._paintImmediately(JComponent.java:4497)
      at javax.swing.JComponent.paintImmediately(JComponent.java:4329)
      at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:407)

      at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(System
      EventQueueUtilities.java:120)
      at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:167)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:437)
      at java.awt.EventDispatchThread.pumpOneEvent(EventDispatchThread.java:15
      0)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)

      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:131)

      at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)

      Any advice/pointers would be very appreciated. The error is "internal" to Java code, so tracking this is a bit crazy ... Is the problem glaring at anyone?

      Thanks!
      Regards,
      Carlos


        • 1. Re: Arrrgh: crazy ArrayIndexOutOfBoundsException with JTable
          807582
          ParamTableModel(reportHeader, 0);
          When I set:

          ParamTableModel(reportHeader, 20);

          Things are better no exception is thrown... I wonder why since even with '0' it seemed the table could have multiple lines as indicated in code snippet 1.

          Is there are problem with the following code?

          Vector tempVector = new Vector();
          Enumeration enum = headerHash.elements();
          Enumeration keys = headerHash.keys();
          while(enum.hasMoreElements()) {
          String pkey = (String) keys.nextElement();
          System.out.println(pkey);
          tempVector.addElement(pkey);
          String pvalue = (String) enum.nextElement();
          System.out.println(pvalue);
          tempVector.addElement(pvalue);
          paramModel.addRow(tempVector);
          // Problem here? ->
          tempVector.removeAllElements();
          /*I want to reuse the same object - is removeAllElements the problem? When this line is there the table is empty even though the vector is "emptied" AFTER filling in each row.*/
          }

          My problem is basic - I have a hashtable and I want the "key" and "value" to fit in 2 columns of a table.

          Thanks!

          • 2. Re: Arrrgh: crazy ArrayIndexOutOfBoundsException with JTable
            807582
            Hi

            I think you've spotted your problem.

            paramModel.addRow(tempVector);
            // Problem here? ->
            tempVector.removeAllElements();


            At this point you have just emptied the vector you added to the tablemodel(tempVector), because your reference to tempVector is still active.

            You may consider (this might make things easier) creating an object that holds your key-value pair and store a reference to it in your HashTable and in the list you use for your tablemodel (btw consider using unsynchronized containers unless you are multithreading)
            that way you have reference updating (i.e. when the object is updated in the vector it is updated in the hastable)

            I hope this helps


            • 3. Re: Arrrgh: crazy ArrayIndexOutOfBoundsException with JTable
              807582
              Thanks! I was afraid that was the problem. It's rectified now. The problem is I just wanted the table to "display" the information in the JTable. The data was not going to be manipulated subsequently, so I wanted the garbage collector to do a clean up. Well, maybe I can do with those extra objects ...

              Thanks again. Cheers!