Solved

pb of allocating aray of string (ArrayIndexOutOfBoundsException)

Posted on 2004-04-15
17
440 Views
Last Modified: 2008-02-01
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
Comment
Question by:laes_
  • 6
  • 4
  • 3
  • +1
17 Comments
 
LVL 5

Expert Comment

by:danielsonchris
ID: 10835097
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
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 10835100
where are you declaring treeLabels as an array?

AW
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 10835116
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
 
LVL 5

Expert Comment

by:danielsonchris
ID: 10835120
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
 
LVL 30

Accepted Solution

by:
Mayank S earned 50 total points
ID: 10839804
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
 
LVL 2

Author Comment

by:laes_
ID: 10840900
thank u all for ur precious pieces of advice
and specially mayankeagle
i'm really grateful mayankeagle
bye
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10840908
Was the problem solved?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 30

Expert Comment

by:Mayank S
ID: 10840916
Ah, I posted that before you accepted the answer. BTW, if it helped, then why a C grade for so much of explanation :-(  ?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10840943
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
 
LVL 2

Author Comment

by:laes_
ID: 10841175
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 10841446
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
 
LVL 2

Author Comment

by:laes_
ID: 10841692
ok
0
 
LVL 2

Author Comment

by:laes_
ID: 10842290
i post a new question about the node selection event,
i look forward to ur response
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 10846994
seems that an upgraded (ooh, bad pun) GRADE is in order here.

AW
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10853154
Thanks Arthur ;-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

929 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now