Error javax.el.Propertynotfoundexception: Array index out of range: 0 Java JSF intersection exercise

Asked

Viewed 58 times

-2

I’m new to programming and new to the Forum. I have a college exercise that I’m not able to do, I need to create a screen called Intersection, which takes 2 intervals and returns a message on screen stating whether or not there is an intersection between the intervals.

This is my manager

    @ManagedBean
    @ViewScoped
    public class InterseccaoManaged implements Serializable {

        private List<Integer> valorA = new ArrayList<Integer>();
        private List<Integer> valorB = new ArrayList<Integer>();

        private Integer tamanho = 0;

        FacesContext context = FacesContext.getCurrentInstance();

        public void verificarInterseccao() {

            for(int i = 0; i < valorA.size(); i++) {
                for(int j = 0; j < valorB.size(); j++) {

                    if(valorB.add(j) == valorA.add(i)) {
                        tamanho ++;

                        FacesMessage facesMessage = new FacesMessage("Existe interseção entre as faixas A e B");
                        context.addMessage(null, facesMessage);
                    }else {
                        FacesMessage facesMessage = new FacesMessage("Não há interseção entre as faixas A e B");
                        context.addMessage(null, facesMessage);
                    }

                }
            }

        }
 ..:/Getters e Setters/:..

Essa Minha Tela

<h:form id="formInterseccao">
            <p:messages severity="info,error,fatal" closable="true"/>
            <div class="ui-fluid">

                <p:panelGrid 
                    columns="2" 
                    columnClasses="ui-grid-col-3,ui-grid-col-3"
                    layout="grid">

                    <h:panelGroup>
                        <p:outputLabel value="Faixa A:"/>
                        <p:inputText value="#{interseccaoManaged.valorA[0]}"/>
                        <p:inputText value="#{interseccaoManaged.valorA[1]}"/>
                    </h:panelGroup>

                    <h:panelGroup>
                        <p:outputLabel value="Faixa B:"/>
                        <p:inputText value="#{interseccaoManaged.valorB[0]}"/>
                        <p:inputText value="#{interseccaoManaged.valorB[1]}"/>
                    </h:panelGroup>

                </p:panelGrid>  

                <h:panelGroup layout="block" style="text-align:center">
                    <p:commandButton 
                        value="Consultar"
                        update="@form" 
                        action="#{lancamentoManaged.verificarInterseccao()}"
                        style="width:120px"/>
                </h:panelGroup>

            </div>
        </h:form>

And the moment I execute is presented the Error

com.sun.faces.context.AjaxExceptionHandlerImpl handlePartialResponseError
GRAVE: javax.el.PropertyNotFoundException: /interseccao.xhtml @30,61 value="#{interseccaoManaged.valorA[0]}": Array index out of range: 0
    at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:100)
    at org.primefaces.util.ComponentUtils.getConverter(ComponentUtils.java:152)
    at org.primefaces.renderkit.InputRenderer.getConvertedValue(InputRenderer.java:199)
    at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1046)
    at javax.faces.component.UIInput.validate(UIInput.java:976)
    at javax.faces.component.UIInput.executeValidate(UIInput.java:1249)
    at javax.faces.component.UIInput.processValidators(UIInput.java:712)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1258)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1258)
    at javax.faces.component.UIForm.processValidators(UIForm.java:253)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1258)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1258)
    at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1195)
    at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at filtro.ConexaoHibernateFilter.doFilter(ConexaoHibernateFilter.java:35)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
Caused by: javax.el.PropertyNotFoundException: Array index out of range: 0
    at javax.el.ListELResolver.getType(ListELResolver.java:51)
    at com.sun.faces.el.DemuxCompositeELResolver._getType(DemuxCompositeELResolver.java:215)
    at com.sun.faces.el.DemuxCompositeELResolver.getType(DemuxCompositeELResolver.java:242)
    at org.apache.el.parser.AstValue.getType(AstValue.java:60)
    at org.apache.el.ValueExpressionImpl.getType(ValueExpressionImpl.java:174)
    at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:98)
    ... 41 more

If you can help me.

Thanks in advance.

1 answer

1

The problem is:

for(int i = 0; i < valorA.size(); i++) {
   for(int j = 0; j < valorB.size(); j++) {

valorA.size() and valorB.size() will be equal to zero since nothing has been added yet. No dendro will enter for. It will have no value to display and indexes 0 and 1 do not exist so the error "Array index out of range: 0". Do a test by putting i < 10 and j < 10 for example. Another thing is that you need the getters and Setter

    public List<Integer> getValorA() {
        return valorA;
    }

    public void setValorA(List<Integer> valorA) {
        this.valorA = valorA;
    }

    public List<Integer> getValorB() {
        return valorB;
    }

    public void setValorB(List<Integer> valorB) {
        this.valorB = valorB;
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.