• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for pointing out my mistake.

It works now. The error message is gone.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic