Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Array Index Out Of Bounds Exception: 0 ( Mistake with My MySQL "count"?)  RSS feed

 
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using the Tomcat and MySQL. Below is the message in the Tomcat log:


----- Root Cause -----
java.lang.ArrayIndexOutOfBoundsException: 0
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:1845)
at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:1056)
at org.apache.commons.dbcp.DelegatingPreparedStatement.setString(DelegatingPreparedStatement.java:217)
at org.apache.artimus.message.dao.MySQLMessageDAO.getNumberOfThreadBeans_forReceiver(MySQLMessageDAO.java:514)
at org.apache.artimus.message.ListThreadHandler.getNumberOfThreads_forReceiver(ListThreadHandler.java:74)
at org.apache.artimus.message.ListThread.execute(ListThread.java:119)



I suppose that the problem is with the method getNumberofThreadBeans_forReceiver (which is line # 514 in the MySQLMessageDAO class). And I also suppose that the "0" caused the ArrayIndexOutOfBoundsException (please point out if I am wrong). However, the method is to "count" the number of records found in the database. And I went to look at my database. I was able to find 3 matches with my eyes.

Please help me to see the mistakes in that method (shown below):
 
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No you are not thinking in the right direction. See the code you have written

public int getNumberOfThreadBeans_forReceiver( String memberName ) throws AssertionException, DatabaseException { Connection conn = null; PreparedStatement stmt = null; String query = "SELECT count(*) FROM message_thread WHERE message_receiver = '" + memberName + "'"; ResultSet rs = null; try { conn = DBConnection.getDBConnection(); stmt = conn.prepareStatement( query ); stmt.setString( 1, memberName ); rs = stmt.executeQuery();



as you can see you have written String query = "SELECT count(*) FROM message_thread WHERE message_receiver = '" + memberName + "'"; and you are using PreparedStatement. You are setting the message_receiver here in this query and then you are trying to use stmt.setString(1,memberName).There is no ? in the query.
So Strange. Isn't it?

Use String query = "SELECT count(*) FROM message_thread WHERE message_receiver = ?"; instead. It will solve the problem.
[ May 20, 2004: Message edited by: Ali Gohar ]
 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for pointing out my mistake.

It works now. The error message is gone.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!