when calling the following function I get an error. The HQL query functions downloading data from the database, but unfortunately I get an error:
java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at com.example.server.RaportGenerator.checkParam(RaportGenerator.java:103)
at com.example.server.RaportGenerator.doGet(RaportGenerator.java:58)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:324)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:829)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:513)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)
And this is my function:
protected boolean checkParam(String login, String sid) {
boolean result = false;
List listOfData;
try {
Session session = HibernateUtil.getSessionFactory().openSession();
Query query = session.createQuery("Select u.login, u.sid from User u Where u.login = :login");
query.setParameter("login",login);
listOfData = query.list();
String sidDb = listOfData.get(1).toString();
if (sid == sidDb) {
result = true;
}
session.close();
} catch (HibernateException e) {
e.printStackTrace();
}
return result;
}
RaportGenerator.java:103 is:
String sidDb = listOfData.get(1).toString();
There are multiple things wrong with this code:
get(0)
to get the first element. listOfData.size()
first to check. result
variable - just return when you know the answer. listOfData
variable can be declared in a tighter scope, which is generally good practice. finally
block, from what I remember. ==
. HibernateException
at this point in your code, and even if you do , you should probably use a better logging mechanism. Here's a code sample with most of this cleaned up.
protected boolean checkParam(String login, String sid) {
Session session = null;
try {
session = HibernateUtil.getSessionFactory().openSession();
Query query = session.createQuery
("Select u.login, u.sid from User u Where u.login = :login");
query.setParameter("login",login);
List listOfData = query.list();
return listOfData.size() == 1
&& listOfData.get(0).toString().equals(sidDb);
} catch (HibernateException e) {
// Do you really just want to print the stack trace to stdout?
// I would probably change the method to allow the exception
// to bubble up...
e.printStackTrace();
} finally {
if (session != null) {
session.close();
}
}
}
You should check the size of listOfData
before trying to get the first item. In this case your query is returning 0 or 1 result. If you mean to get the first item you should change to get(0)
. Regardless you should code to handle the query returning nothing.
It looks like you also have a mistake in how you are comparing the result.
String sidDb = listOfData.get(1).toString();
if (sid == sidDb) {
result = true;
}
Should be changed to.
String sidDb = listOfData.get(0).getSid()
if (sid.equals(sidDb)) {
result = true;
}
Your query is not returning any rows from the database, therefore listofData does not contain any objects and its length =0 . When you make the call to
String sidDb = listOfData.get(1).toString();
Your asking the array to return an object that does not exist, therefore there is no index for the object in the array.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.