?
Solved

help to debug exception

Posted on 2007-07-23
9
Medium Priority
?
210 Views
Last Modified: 2010-08-05
Hello there,

I am getting is error when i traverse on a Jtree.what could be the problem.please throw some light on it.

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
      at java.util.Vector.get(Unknown Source)
      at src.com.victa.TreeTableInternalFrame.initParentsTables(TreeTableInternalFrame.java:3542)
      at src.com.victa.TreeTableInternalFrame.access$29(TreeTableInternalFrame.java:3535)
      at src.com.victa.TreeTableInternalFrame$1.mousePressed(TreeTableInternalFrame.java:341)
      at java.awt.AWTEventMulticaster.mousePressed(Unknown Source)
      at java.awt.Component.processMouseEvent(Unknown Source)
      at javax.swing.JComponent.processMouseEvent(Unknown Source)
      at java.awt.Component.processEvent(Unknown Source)
      at java.awt.Container.processEvent(Unknown Source)
      at java.awt.Component.dispatchEventImpl(Unknown Source)
      at java.awt.Container.dispatchEventImpl(Unknown Source)
      at java.awt.Component.dispatchEvent(Unknown Source)
      at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
      at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
      at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
      at java.awt.Container.dispatchEventImpl(Unknown Source)
      at java.awt.Window.dispatchEventImpl(Unknown Source)
      at java.awt.Component.dispatchEvent(Unknown Source)
      at java.awt.EventQueue.dispatchEvent(Unknown Source)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.run(Unknown Source)

this is a block of code for line 341.the line is indicated in the code below.
case BaseRow.LINECARD_TYPE : {
                                           changeCard(rightCardPanel, CITY_CO_DSLAM_PANEL);
                                           Vector col = getNodesThisLevel(linecardCollect, select);
                                           int selIndex = Collections.binarySearch(col, select, new PrimaryKeyComparator());
                                           
                                           if (!isSameBranch(select,lastSelectedNode))
                                           {
                                                 removeAllRows(linecardTable);
                                                 initLinecardTable(col);
                                           }
                                           linecardTable.setRowSelectionInterval(selIndex, selIndex);
                                           tabPane.setSelectedIndex(3);
                                           initParentsTables(select);      ///////////line341////////////
                                           break;
                                     }

block of code between line 3535 - 3542

private void initParentsTables(DefaultMutableTreeNode selNode) {
          BaseRow selRow = (BaseRow)selNode.getUserObject();
          
          int index = -1;
          switch (selRow.getType()) {
                case BaseRow.LINECARD_TYPE :
                      index = findParentElement(dslamCollect, selNode);
                      DefaultMutableTreeNode pNode = (DefaultMutableTreeNode)dslamCollect.get(index);  /////////////////////////////////line 3542//////////////////////
                      Vector cod = getNodesThisLevel(dslamCollect, pNode);
                  
                      index = findParentElement(cod, selNode);
                      removeAllRows(dslamTable);
                      initDslamTable(cod);
                      dslamTable.setRowSelectionInterval(index, index);
                      selNode = pNode;
                  
                case BaseRow.DSLAM_TYPE :
                      index = findParentElement(coCollect, selNode);
                      pNode = (DefaultMutableTreeNode)coCollect.get(index);
                      Vector cov = getNodesThisLevel(coCollect, pNode);
                      
                      index = findParentElement(cov, selNode);
                      removeAllRows(coTable);
                      initCoTable(cov);
                      coTable.setRowSelectionInterval(index, index);
                      selNode = pNode;
          
                case BaseRow.CO_TYPE :
                      if(privilege==1)
                      {
                            index = findParentElement(cityCollect, selNode);
                      }
                      else if(privilege==2 || privilege==3)
                      {
                            index = findParentElement(cityPrivilegeCollect, selNode);
                      }
                      cityTable.setRowSelectionInterval(index, index);
                      int coId = ((CoRow)selNode.getUserObject()).getCoId().intValue();
                         thnPanel.clear();
                         fillThumbnails(true, coId);
                         thnPanel.sort();
                         thnPanel.recountOnResize(jspPhoto.getViewport().getWidth());
                         thnPanel.repaint();
          }
    }
0
Comment
Question by:zolf
9 Comments
 
LVL 3

Expert Comment

by:asood314
ID: 19553525
Check to make sure that index != -1 before passing it to any functions.  That seems to be the most likely source of the exception.
0
 
LVL 92

Assisted Solution

by:objects
objects earned 880 total points
ID: 19553569
>                       index = findParentElement(dslamCollect, selNode);

its failing to find the index of the parent element, check that method
0
 
LVL 12

Accepted Solution

by:
keyurkarnik earned 1120 total points
ID: 19553794
The findParentElement(dslamCollect, selNode) is returning -1

Due to this, dslamCollect.get(index) is becoming dslamCollect.get(-1), hence the ArrayIndexOutOfBounds exception.

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:keyurkarnik
ID: 19553795
Either try debugging through the findParentElement or try pasting the code here!!
0
 

Author Comment

by:zolf
ID: 19553925

private int findParentElement(Vector collect, DefaultMutableTreeNode node) {
          BaseRow row = (BaseRow)node.getUserObject();
          int index = -1;
          for (int i = 0; i < collect.size(); i++) {
                DefaultMutableTreeNode currNode = (DefaultMutableTreeNode)collect.get(i);
                BaseRow currRow = (BaseRow)currNode.getUserObject();
                if (row.getForeignKey() == currRow.getPrimaryKey()) {
                      index = i;
                      break;
                }
          }
          return index;
    }
0
 
LVL 92

Expert Comment

by:objects
ID: 19553945
>                 if (row.getForeignKey() == currRow.getPrimaryKey()) {

try

                if (row.getForeignKey().equals(currRow.getPrimaryKey())) {
0
 
LVL 12

Expert Comment

by:keyurkarnik
ID: 19553948
if collect.size() == 0,
it will never enter the for loop

So, the index returned will be -1.

So the only thing you can do is this :

index = findParentElement(dslamCollect, selNode);
if(index != -1)
{

continue with your code here

}

0
 
LVL 12

Expert Comment

by:keyurkarnik
ID: 19553958
the other possibility is that you are entering the for loop, in which case, the

 if (row.getForeignKey() == currRow.getPrimaryKey())

is returning false


so either that is the case, or you could try

row.getForeignKey().equals( currRow.getPrimaryKey())
0
 
LVL 12

Expert Comment

by:keyurkarnik
ID: 19553965
whichever case it might be, there is always a possiblity of the method returning a -1 (if the size is 0 or if the keys are unequal)


So you should check for the -1 case wherever you are calling the method
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question