Link to home
Start Free TrialLog in
Avatar of laes_
laes_

asked on

pb of allocating aray of string (ArrayIndexOutOfBoundsException)

pb of allocating array of string ,dont pay attention to the connection ,it works ,the pb is
allocation of the array
      String[] closedItems = new String[50];
      ResultSet rs=st.executeQuery(requete);
      int i=0;

      while(rs.next())
      {

            treeLabels[i]=new String(rs.getString("name"));
        i++;
      }

the error is est java.lang.ArrayIndexOutOfBoundsException
help me please

Avatar of danielsonchris
danielsonchris

Should be a fairly simple fix...
Make sure to run a check on the
closedItems.Length property Or use a constant variable set as follows:

const MAX_SIZE_CLOSED_ITEMS = 50

String[] closedItems = new String[MAX_SIZE_CLOSED_ITEMS];
ResultSet rs=st.executeQuery(requete);
int i=0;
while(rs.next())
{
    if ( i++ >= MAX_SIZE_CLOSED_ITEMS ) break;
    treeLabels[i]=new String(rs.getString("name"));
}
where are you declaring treeLabels as an array?

AW
should your line be refering to closedItems[i] and not treeLables[i]?
:

String[] closedItems = new String[MAX_SIZE_CLOSED_ITEMS];
ResultSet rs=st.executeQuery(requete);
int i=0;
while(rs.next())
{
    if ( i++ >= MAX_SIZE_CLOSED_ITEMS ) break;
    treeLabels[i]=new String(rs.getString("name"));
}


should that be:

String[] closedItems = new String[MAX_SIZE_CLOSED_ITEMS];
ResultSet rs=st.executeQuery(requete);
int i=0;
while(rs.next())
{
    if ( i++ >= MAX_SIZE_CLOSED_ITEMS ) break;
    closedItems[i]=new String(rs.getString("name"));
}



???
P.S.  
the closedItems string array.. should that be named "treeLabels"?   In either case you need to protect the size of that array.  In java there is a "length" property.  Use that and make sure you do not exceed the length.
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of laes_

ASKER

thank u all for ur precious pieces of advice
and specially mayankeagle
i'm really grateful mayankeagle
bye
Was the problem solved?
Ah, I posted that before you accepted the answer. BTW, if it helped, then why a C grade for so much of explanation :-(  ?
C grade is not given if the expert provided a complete answer (which I think that I did for the amount that you'd asked - I gave you three alternatives). Do you know enough about closing: https://www.experts-exchange.com/help.jsp#hi73
Avatar of laes_

ASKER

it is not the real pb for me mayankeagle
it is just pb of allocating memory !
my pb really is about mouse event
i use  a split pane
JSplitPane sp = new JSplitPane(1,new tree(),jscrollpane);
one part there is a tree  witch display all the table that exist in the database in the other par i show the content of table the user select
this is the code of tree class
DefaultMutableTreeNode[] closedNodes = new
                        DefaultMutableTreeNode[closedItems.length];
for (i = 0; i < closedItems.length; i++) {
                  closedNodes[i] = new
                        DefaultMutableTreeNode(closedItems[i]);
                  nodes[1].add(closedNodes[i]);
            }
i want that once the user click on the branch of the table in the other part of the split i show the content of table
the event of clicking on the node ,and informing the class sgbd in witch the splitpane exit
and the sgbd must inform the class jscrollpane witch contain the display of the table ,the name of node that has been selected
so help me how i will solve it ,and be sure u will have excellent response







That is a different question from this one. You mentioned nothing of this when you asked the question, and now you are stating this after closing the question. Ideally, you should post a new question for that. I will definitely post on that one if you do so.
Avatar of laes_

ASKER

ok
Avatar of laes_

ASKER

i post a new question about the node selection event,
i look forward to ur response
seems that an upgraded (ooh, bad pun) GRADE is in order here.

AW
Thanks Arthur ;-)