警告:java.lang.IndexOutOfBoundsException
调用以下函数时出现错误。 HQL查询函数从数据库下载数据,但不幸的是我得到一个错误:
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)
这是我的函数:
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();
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此代码存在多个问题:
get(0)
来获取 first 元素。listOfData.size()
进行检查。result
变量 - 只需在知道答案时返回即可。listOfData
变量可以在更严格的范围内声明,这通常是一个很好的做法。finally
块中关闭会话。==
测试字符串相等性。HibernateException
,即使您捕获,您也应该使用更好的日志记录机制。这是一个代码示例,其中大部分已清理。
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.
在尝试获取第一项之前,您应该检查
listOfData
的大小。在这种情况下,您的查询将返回 0 或 1 个结果。如果您想获取第一个项目,则应更改为get(0)
。无论如何,您应该编写代码来处理不返回任何内容的查询。看来您在比较结果的方式上也有错误。
应改为.
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 toget(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.
Should be changed to.
您的查询不会从数据库返回任何行,因此 listofData 不包含任何对象且其长度 =0 。当您调用
Your 要求数组返回一个不存在的对象时,因此数组中没有该对象的索引。
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
Your asking the array to return an object that does not exist, therefore there is no index for the object in the array.