警告:java.lang.IndexOutOfBoundsException

发布于 2024-11-28 00:43:18 字数 2581 浏览 0 评论 0原文

调用以下函数时出现错误。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半岛未凉 2024-12-05 00:43:18

此代码存在多个问题:

  • 您要求元素 1,它是第二个元素。我怀疑您实际上的意思是 get(0) 来获取 first 元素。
  • 当然,如果用户的登录名不存在,那仍然会失败......您应该首先使用 listOfData.size() 进行检查。
  • 您不需要 result 变量 - 只需在知道答案时返回即可。
  • listOfData 变量可以在更严格的范围内声明,这通常是一个很好的做法。
  • 据我所知,您可能应该在finally块中关闭会话。
  • 您不应该使用 == 测试字符串相等性。
  • 此时您可能不应该在代码中捕获 HibernateException,即使您捕获,您也应该使用更好的日志记录机制。

这是一个代码示例,其中大部分已清理。

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();
        }
    }
}

There are multiple things wrong with this code:

  • You're asking for element 1, which is the second element. I suspect you actually meant get(0) to get the first element.
  • That will still fail if the user's login doesn't exist, of course... you should use listOfData.size() first to check.
  • You don't need the result variable - just return when you know the answer.
  • The listOfData variable can be declared in a tighter scope, which is generally good practice.
  • You should probably be closing the session in a finally block, from what I remember.
  • You shouldn't be testing string equality with ==.
  • You probably shouldn't be catching 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();
        }
    }
}
煮酒 2024-12-05 00:43:18

在尝试获取第一项之前,您应该检查 listOfData 的大小。在这种情况下,您的查询将返回 0 或 1 个结果。如果您想获取第一个项目,则应更改为 get(0)。无论如何,您应该编写代码来处理不返回任何内容的查询。

看来您在比较结果的方式上也有错误。

        String sidDb = listOfData.get(1).toString();
        if (sid == sidDb) {
            result = true;  
        }

应改为.

     String sidDb = listOfData.get(0).getSid()
     if (sid.equals(sidDb)) {
         result = true;  
     }

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;  
     }
自此以后,行同陌路 2024-12-05 00:43:18

您的查询不会从数据库返回任何行,因此 listofData 不包含任何对象且其长度 =0 。当您调用

String sidDb = listOfData.get(1).toString();

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文