Solved

pb of allocating aray of string (ArrayIndexOutOfBoundsException)

Posted on 2004-04-15
17
439 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
Comment Utility
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
Comment Utility
where are you declaring treeLabels as an array?

AW
0
 
LVL 44

Expert Comment

by:Arthur_Wood
Comment Utility
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
Comment Utility
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:
mayankeagle earned 50 total points
Comment Utility
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_
Comment Utility
thank u all for ur precious pieces of advice
and specially mayankeagle
i'm really grateful mayankeagle
bye
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
Was the problem solved?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
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:mayankeagle
Comment Utility
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_
Comment Utility
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:mayankeagle
Comment Utility
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_
Comment Utility
ok
0
 
LVL 2

Author Comment

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

Expert Comment

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

AW
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
Thanks Arthur ;-)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem to setup 18 78
Order table with macro 3 74
hasOne  challenge 59 87
Counting documents in a Domino View 3 31
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
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 fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

772 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

9 Experts available now in Live!

Get 1:1 Help Now