Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 462
  • Last Modified:

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

0
laes_
Asked:
laes_
  • 6
  • 4
  • 3
  • +1
1 Solution
 
danielsonchrisCommented:
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"));
}
0
 
Arthur_WoodCommented:
where are you declaring treeLabels as an array?

AW
0
 
Arthur_WoodCommented:
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"));
}



???
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
danielsonchrisCommented:
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.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
First off, the problem is happening because in a Java ResultSet, it is not possible to find out the number of rows in the ResultSet. There are many ways to fix that.

The first option is to use sun.jdbc.rowset.CachedRowSet - an implementation of RowSet with similar methods as ResultSet. It has a size () method which will give you the size of the CachedRowSet. The other methods which it has have the same syntax as ResultSet methods and so, it should not be a problem.

ResultSet rs = st.executeQuery ( requete ) ;

CachedRowSet crs = new CachedRowSet () ;
crs.populate ( rs ) ;
rs.close () ; // NOTE THIS - you can even close the connection now as CachedRowSet is like a disconnected RowSet

// I don't know what for you were declaring the closedItems [] array, and why it always had a size of 50

treelabels = new String[crs.size ()] ; // ALLOCATE AS MANY ITEMS AS MANY ARE THERE in the CachedRowSet
i = 0 ;

while ( crs.next () )
  treelabels[i ++] = new String ( crs.getString ( "name" ) ) ;
 

Now, if you don't want to use CachedRowSet, then you can try 2 different options: Either go the hard way of scrolling through the ResultSet and finding out the size, and then again scrolling through it right from the start.

ResultSet rs = st.executeQuery ( requete ) ;
int count = 0 ;

while ( rs.next () )
  count ++ ; // end while

rs.first () ; // MOVE to the first record in the ResultSet

// Note that you can do the above only if the Statement st is obtained using something like:
// st = connection.createStatement ( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY, ResultSet.CLOSE_CURSORS_AT_COMMIT )

// if you simply use: st = connection.createStatement (), then the ResultSet's first () method cannot be used because it wil
// be a forward-only ResultSet -> in that case, you need to obtain the ResultSet again, using:
// rs = st.executeQuery ( requete ) ;

treelabels = new String[count] ; // ALLOCATE AS MANY ITEMS AS MANY ARE THERE in the ResultSet
i = 0 ;

while ( rs.next () )
  treelabels[i ++] = new String ( rs.getString ( "name" ) ) ;
 

A better option is to use some Collection, so that you don't have to scroll back or even find the size, like:

Vector v = new Vector () ; // you can use ArrayList - it is faster, but remember that it is not synchronized
rs = st.executeQuery ( requete ) ;
treelabels = new String[count] ;
i = 0 ;

while ( rs.next () )
  v.add ( new String ( rs.getString ( "name" ) ) ) ;

You can access the Vector like:

for ( int i = 0, iSize = v.size () ; i < iSize ; i ++ )
  String temp = ( String ) v.get ( i ) ; // end for
0
 
laes_Author Commented:
thank u all for ur precious pieces of advice
and specially mayankeagle
i'm really grateful mayankeagle
bye
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Was the problem solved?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Ah, I posted that before you accepted the answer. BTW, if it helped, then why a C grade for so much of explanation :-(  ?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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: http://www.experts-exchange.com/help.jsp#hi73
0
 
laes_Author Commented:
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







0
 
Mayank SAssociate Director - Product EngineeringCommented:
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.
0
 
laes_Author Commented:
ok
0
 
laes_Author Commented:
i post a new question about the node selection event,
i look forward to ur response
0
 
Arthur_WoodCommented:
seems that an upgraded (ooh, bad pun) GRADE is in order here.

AW
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Thanks Arthur ;-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 6
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now