2

每当我运行我的 xhtml 时。它给了我以下例外。statusindex 对象的值为 5。我使用 JQuery 进行延迟滚动,所以当我的 xhml 页面 getMoreStatusList 调用 getMoreStatusList 函数时,它给了我一个索引超出范围的异常。数据库中有 26 个状态更新,因此索引越界异常对我来说毫无意义。

1) 代码

public List<Status> getMoreStatusList() {

    FacesContext context = FacesContext.getCurrentInstance();
    HttpSession session = (HttpSession) context.getExternalContext()
            .getSession(false);
    User user = (User) session.getAttribute("userdet");
    Query query = em.createQuery("SELECT s FROM Status s WHERE s.email='"
            + user.getEmail() + "' ORDER BY s.timeMillis desc",
            Status.class);
    List<Status> results = query.getResultList();
    Query query1 = em.createQuery("SELECT f FROM Friend f WHERE f.email='"
            + user.getEmail() + "'", Friend.class);
    List<Friend> results1 = query1.getResultList();
    Iterator<Friend> it = results1.listIterator();
    while (it.hasNext()) {
        String email = it.next().getFriendEmail();
        Query query2 = em.createQuery(
                "SELECT s FROM Status s WHERE s.email='" + email
                        + "' ORDER BY s.timeMillis desc", Status.class);
        List<Status> results2 = query2.getResultList();
        results.addAll(results2);

    }
    Collections.sort(results);
    int index = (int) session.getAttribute("statusindex");
    System.out.println(index);
    results = results.subList(index,index+5);
    session.setAttribute("statusindex", index + 5);
    return results;

}

2) 例外

Mar 02, 2013 9:31:58 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/FreeBird_v.6] threw exception [/getMoreStatusList.xhtml @120,43 test="#{p.videostatus!=null}" /getMoreStatusList.xhtml @120,43 test="#{p.videostatus!=null}": /getMoreStatusList.xhtml @14,60 items="#{statusBean.moreStatusList}": Error reading 'moreStatusList' on type com.bean.StatusBean] with root cause
java.lang.IndexOutOfBoundsException: toIndex = 30
    at java.util.SubList.<init>(Unknown Source)
    at java.util.RandomAccessSubList.<init>(Unknown Source)
    at java.util.AbstractList.subList(Unknown Source)
    at java.util.Vector.subList(Unknown Source)
    at com.bean.StatusBean.getMoreStatusList(StatusBean.java:190)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:87)
    at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67)
    at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:71)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:169)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:106)
    at com.sun.faces.facelets.tag.jstl.core.IndexedValueExpression.getValue(IndexedValueExpression.java:89)
    at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:68)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:147)
    at org.apache.el.parser.AstNotEqual.getValue(AstNotEqual.java:38)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:106)
    at com.sun.faces.facelets.tag.TagAttributeImpl.getObject(TagAttributeImpl.java:326)
    at com.sun.faces.facelets.tag.TagAttributeImpl.getBoolean(TagAttributeImpl.java:133)
    at com.sun.faces.facelets.tag.jstl.core.IfHandler.apply(IfHandler.java:88)
    at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:94)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:131)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:162)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:114)
    at com.sun.faces.facelets.tag.jstl.core.ForEachHandler.apply(ForEachHandler.java:214)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:131)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:162)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:114)
    at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:94)
    at com.sun.faces.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:89)
    at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:94)
    at com.sun.faces.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:79)
    at com.sun.faces.facelets.impl.DefaultFacelet.apply(DefaultFacelet.java:148)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.buildView(FaceletViewHandlingStrategy.java:734)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:96)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:135)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:309)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
4

3 回答 3

5

List.subList()的文档非常清楚:

抛出:IndexOutOfBoundsException- 对于非法的端点索引值 ( fromIndex < 0 || toIndex > size || fromIndex > toIndex)

在你的情况下,toIndex > size

java.lang.IndexOutOfBoundsException: toIndex = 30
于 2013-03-02T16:30:32.853 回答
4

消息说toIndex是 30。这意味着statusindex不是 5,如您所相信的那样,而是 25:

java.lang.IndexOutOfBoundsException: toIndex = 30

旁注,您应该在查询中使用命名参数而不是字符串连接来传递参数。您的代码容易受到 SQL 注入攻击。如果电子邮件地址恰好包含单引号,它也会失败。

于 2013-03-02T16:29:15.637 回答
2

results = results.subList(index,index+5);

所以它会这样做:

results.subList(25,30);如果通过了 25,则错误。

于 2013-03-02T16:29:06.683 回答