处理视图上的数组时出现ArrayIndexOutOfBoundsException

时间:2013-05-20 13:26:47

标签: jsf primefaces

我的问题是如何在我的视图中显示一个集合: 例如:

<h:outputText value="#{item.machineCollection.get(0).id != null ? item.machineCollection.get(0).id : ''}"/>

即使在测试我的代码后如果结果为null我也会收到此错误

  

数组索引超出范围:0

     

java.lang.ArrayIndexOutOfBoundsException:数组索引超出范围:0     在java.util.Vector.get(Vector.java:694)at   org.eclipse.persistence.indirection.IndirectList.get(IndirectList.java:401)     在sun.reflect.GeneratedMethodAccessor194.invoke(未知来源)at   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)     在java.lang.reflect.Method.invoke(Method.java:597)at   javax.el.BeanELResolver.invokeMethod(BeanELResolver.java:779)at at   javax.el.BeanELResolver.invoke(BeanELResolver.java:528)at at   javax.el.CompositeELResolver.invoke(CompositeELResolver.java:257)at at   com.sun.el.parser.AstValue.getValue(AstValue.java:134)at at   com.sun.el.parser.AstValue.getValue(AstValue.java:183)at at   com.sun.el.parser.AstNotEqual.getValue(AstNotEqual.java:58)at at   com.sun.el.parser.AstChoice.getValue(AstChoice.java:64)at   com.sun.el.parser.AstDeferredExpression.getValue(AstDeferredExpression.java:63)     在   com.sun.el.parser.AstCompositeExpression.getValue(AstCompositeExpression.java:68)     在   com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:224)     在   org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)     在   com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)     在   javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)     在   javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)     在javax.faces.component.UIOutput.getValue(UIOutput.java:169)at   com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205)     在   com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:355)     在   com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:164)     在   javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)     在javax.faces.component.UIComponent.encodeAll(UIComponent.java:1764)     在javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)     在   org.primefaces.component.datatable.DataTableRenderer.encodeRegularCell(DataTableRenderer.java:741)     在   org.primefaces.component.datatable.DataTableRenderer.encodeRow(DataTableRenderer.java:693)     在   org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:601)     在   org.primefaces.component.datatable.DataTableRenderer.encodeRegularTable(DataTableRenderer.java:220)     在   org.primefaces.component.datatable.DataTableRenderer.encodeMarkup(DataTableRenderer.java:192)     在   org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:74)     在   javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)     在   org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:59)     在   org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:43)     在   org.primefaces.component.panel.PanelRenderer.encodeContent(PanelRenderer.java:198)     在   org.primefaces.component.panel.PanelRenderer.encodeMarkup(PanelRenderer.java:115)     在   org.primefaces.component.panel.PanelRenderer.encodeEnd(PanelRenderer.java:55)     在   javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)     在javax.faces.component.UIComponent.encodeAll(UIComponent.java:1764)     在javax.faces.render.Renderer.encodeChildren(Renderer.java:168)at   javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)     在   org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:55)     在   org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:43)     在   org.primefaces.component.layout.LayoutUnitRenderer.encodeEnd(LayoutUnitRenderer.java:51)     在   javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)     在javax.faces.component.UIComponent.encodeAll(UIComponent.java:1764)     在javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)     在javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)     在javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)     在   com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)     在   com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)     在   javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:288)     在   com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)     在com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)at   com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)     在javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)at   org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)     在   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)     在   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)     在   org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)     在   org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)     在   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)     在   org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)     在   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)     在   com.sun.enterprise.v3.services.impl.ContainerMapper $ AdapterCallable.call(ContainerMapper.java:317)     在   com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)     在   com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)     在   com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)     在   com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)     在   com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)     在   com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)     在   com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)     在   com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)     在   com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)     在   com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)     在   com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)     在com.sun.grizzly.ContextTask.run(ContextTask.java:71)at   com.sun.grizzly.util.AbstractThreadPool $ Worker.doWork(AbstractThreadPool.java:532)     在   com.sun.grizzly.util.AbstractThreadPool $ Worker.run(AbstractThreadPool.java:513)     在java.lang.Thread.run(Thread.java:680)

如果某人有解决方案,请紧急请求

2 个答案:

答案 0 :(得分:2)

问题是你有

item.machineCollection.get(0).id

如果您有一个空集合,item.machineCollection.get(0)将无效。

在primeface数据表中,您可以使用var属性。因此,如果您添加var="row",则可以执行

<p:datatable var="row" value="#{item.machineCollection}" >
    <p:column>
        <f:facet name="header">
            <h:outputText value="Id" />
        </f:facet>
        <h:outputText value="#{row.id}"/>
    </p:column>
....
</p:datatable>

答案 1 :(得分:0)

使用非空运算符:

<h:outputText value="#{not empty item.machineCollection ? item.machineCollection.get(0).id : ''}

或编写自定义JSF函数,如本主题所述:

How to create a custom EL function to invoke a static method?