File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Array index out of bounds exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Array index out of bounds exception" Watch "Array index out of bounds exception" New topic
Author

Array index out of bounds exception

A Joseph
Greenhorn

Joined: Dec 10, 2010
Posts: 7
Hi All,

I am working on this small project and am having an exception thrown. I had never seen this before even though this code had worked before without problems. First, here is the code



Here is where the ArrayIndexOutOfBound exception is being thrown. This is the trace,



What I don't understand is the cause being null. I have added that System.out.println statement to print the result to the console and it is fine. It is not null and it retrieves the right item from the database. The database is connecting too, so this has me baffled . I will leave the code from the call to the database as well perhaps that has something to do with it.



I tried changing the null return to return something else but no luck. Any help is appreciated. Happy holidays.
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
It would help a lot if you provide the full stack trace. Instead of which is not very useful, try
A Joseph
Greenhorn

Joined: Dec 10, 2010
Posts: 7
Sorry here is the full stack trace.

Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
So the exception is caused by the HRES_Edit_Employee.getEmployeeInfo() method. Can you post that code?
A Joseph
Greenhorn

Joined: Dec 10, 2010
Posts: 7
Here is the method.... forgive the ugliness of it



The commented out print to the console works the item from the database is there. I assume its an issue with the row.elementAt() stuff but again this code worked today and has not changed. So I am at a loss as to why this stopped working. Thanks for looking at it though.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Its always better to use the index after checking if the length of the Vector is >= the index. I would suggest using a loop to get the elements from the Vector.

By the way- which is this line at HRES_Edit_Employee.getEmployeeInfo(HRES_Edit_Employee.java:238) pointing to in the method- getEmployeeInfo


Mohamed Sanaulla | My Blog
wilson marasigan
Greenhorn

Joined: Dec 24, 2010
Posts: 4
Some of the fields in the return SQL return a null value. You should check which one is which. The observation I had is that you return null from the method. One of the effective way it to return an empty array, so that you wont encounter any surprises or exception during runtime. This is a very good practice in coding though.
A Joseph
Greenhorn

Joined: Dec 10, 2010
Posts: 7
Hi Mohamed, The line that the exception is pointing to is this



I tried putting in a loop and I get the right selection from the database. Its very perplexing.

Hi Wilson, I did notice that and tried changing it but I still get the same error. Thank you though.
wilson marasigan
Greenhorn

Joined: Dec 24, 2010
Posts: 4
1. public Vector<Vector<String>>get_Row_From_Database(String tableName, String columnName, String item){
2. Vector <Vector<String>> row = new Vector <Vector<String>> ();
3. Vector <String> rowData;
4. ResultSet rs = null;
5. Statement sm = null;
6. ResultSetMetaData rsmd;
7. try{
8. if(connected){
9. sm = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
10. String query = "select * from " + tableName + " where " + columnName + " = " + "'" + item + "'";
11. rs = sm.executeQuery(query);
12. rsmd = rs.getMetaData();
13. //Cannot seem to get rid of this code as the Viewer wont display whats in the columns without it
14. numberOfColumns = rsmd.getColumnCount();
15. row = new Vector <Vector<String>> ();
16. while(rs.next()){
17. rowData = new Vector<String>(numberOfColumns);
18. for(int j = 1; j <= numberOfColumns; j++){
19. rowData.add(rs.getString(j));
20. }
21. row.add(rowData);
22. }
23. return row;
24. }
25. }catch(SQLException se){
26. displayErrors(se);
27. }
28. return row;
29. }
.....

# try{
# result = sql.get_Row_From_Database(tableName, columnName, item);
# if (!result.isEmpty()) {
# System.out.println(result);
# hres.getEmployeeInfo(result, columnName, item);
# hres.display();
# }
# }catch(ArrayIndexOutOfBoundsException ae){
# JOptionPane.showMessageDialog(null, "The employee is not in the database", "Information",
# JOptionPane.OK_OPTION);
# System.out.println(ae.getCause() + " " + ae.getLocalizedMessage() + " " + ae.getStackTrace());
# }
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

You have to check for the size of the Vector before trying to access the elements at different index. There may be cases when the Size of the Vector is less than the index you have specified.
A Joseph
Greenhorn

Joined: Dec 10, 2010
Posts: 7
Hi Mohamed,

Yes you are right it is the fact that the size is wrong. I am only getting the first column not the whole row. Very weird that this was working before.

When I print this line to the console



I get the whole row. But when I have the old code



at the firstNameField I get that element, but after that nothing. So I believe that is what is causing the error.
A Joseph
Greenhorn

Joined: Dec 10, 2010
Posts: 7
Thank you all, I solved it. The first row.elementAt() was 1 I switched it to 0 and it works perfectly. Thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array index out of bounds exception