Erro java.lang.IndexOutOfBoundsException: toIndex =

4 respostas
vitorkgb
Estou fazendo o exemplo abaixo mas dá o erro do título desse tópico
http://www.k19.com.br/downloads/apostilas-java/k19-k12-desenvolvimento-web-com-jsf2-e-jpa2

segue o código

index.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">

<h:head>
    <title>Gerador de Apostas</title>
</h:head>
<h:body>
    <h:form>
    <h:panelGrid>
      <h:outputLabel value="Quantidade total de números:"/>
      <h:inputText value="#{geradorDeAposta.quantidadeDeNumeros}"/>

      <h:outputLabel value="Quantidade de números por aposta:"/>
      <h:inputText value="#{geradorDeAposta.tamanhoDaAposta}"/>

      <h:outputLabel value="Quantidade de apostas:"/>
      <h:inputText value="#{geradorDeAposta.quantidadeDeApostas}"/>

      <h:commandButton action="#{geradorDeAposta.geraApostas}" value="Gerar"/>
   </h:panelGrid>
   </h:form>
</h:body>
</html>
GeradorDeAposta.java
package modelo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.faces.bean.ManagedBean;

@ManagedBean

public class GeradorDeAposta implements Serializable
{
   private int quantidadeDeNumeros;
   private int tamanhoDaAposta;
   private int quantidadeDeApostas;
   private List<List<Integer>> apostas;

    public int getQuantidadeDeApostas() {
        return quantidadeDeApostas;
    }

    public void setQuantidadeDeApostas(int quantidadeDeApostas) {
        this.quantidadeDeApostas = quantidadeDeApostas;
    }

    public int getQuantidadeDeNumeros() {
        return quantidadeDeNumeros;
    }

    public void setQuantidadeDeNumeros(int quantidadeDeNumeros) {
        this.quantidadeDeNumeros = quantidadeDeNumeros;
    }

    public int getTamanhoDaAposta() {
        return tamanhoDaAposta;
    }

    public void setTamanhoDaAposta(int tamanhoDaAposta) {
        this.tamanhoDaAposta = tamanhoDaAposta;
    }


  public String geraApostas()
  {
      // Prepara uma lista com todos os números
      ArrayList<Integer> numeros = new ArrayList<Integer>();
      for (int j = 1; j <= this.quantidadeDeNumeros; j++)
      {
            numeros.add(j);
      }

      // Cria uma sublista da lista de números
      List<Integer> subList = numeros.subList(0, this.tamanhoDaAposta);

      // Lista de apostas vazia
      this.apostas = new ArrayList<List<Integer>>();

      // Gera as apostas
      for (int i = 0; i < this.quantidadeDeApostas; i++)
      {
          Collections.shuffle(numeros);
          List<Integer> aposta = new ArrayList<Integer>(subList);
          this.apostas.add(aposta);
      }
      return "lista-de-apostas";
 }


}
lista-de-aposta.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
 xmlns:h="http://java.sun.com/jsf/html"
 xmlns:ui="http://java.sun.com/jsf/facelets">

<h:head>
     <title>Gerador de Apostas</title>
</h:head>
<h:body>
     <ui:repeat var="aposta" value="#{geradorDeAposta.apostas}" varStatus="status">
     <h:outputText value="Aposta #{status.index + 1}: "/>
     <h:outputText value="#{aposta}"/>
     <br/>
     </ui:repeat>
</h:body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

4 Respostas

ivo_costa

O erro pode estar ocorrendo nessa linha:
List<Integer> subList = numeros.subList(0, this.tamanhoDaAposta);

Debuga e veja se a variavel numeros tem elementos suficientes para a sub lista.

vitorkgb

o código está certo, na verdade eu estava preenchendo errado o formulário, colocando numeros baixos…
além do mais estava faltando o get e set de apostas

como deleto um tópico?

drigo.angelo

não delete o tópico, pode servir pra outras pessoas com a mesma dúvida :stuck_out_tongue:

J

Alguém pode me ajudar?
Estou com o mesmo problema do vitorkgb e no mesmo código, fiz tudo conforme consta na apostila da K19 (http://k19.com.br/downloads/apostilas-java/k19-k12-desenvolvimento-web-com-jsf2-e-jpa2). A única diferença está no meu arquivo web.xml, conforme consta abaixo.

web.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>K19-Loteria</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.xhtml</url-pattern> </servlet-mapping> </web-app>

Log do Glassfish.

[#|2011-08-16T00:05:56.258-0300|WARNING|glassfish3.0.1|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=26;_ThreadName=http-thread-pool-8080-(1);|StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception

javax.faces.el.EvaluationException: java.lang.IndexOutOfBoundsException: toIndex = 6

at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)

at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)

at javax.faces.component.UICommand.broadcast(UICommand.java:315)

at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775)

at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)

at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)

at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)

at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)

at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)

at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)

at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)

at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)

at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)

at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)

at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)

at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)

at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)

at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)

at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)

at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)

at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)

at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)

at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)

at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)

at com.sun.grizzly.ContextTask.run(ContextTask.java:69)

at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)

at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)

at java.lang.Thread.run(Thread.java:662)

Caused by: java.lang.IndexOutOfBoundsException: toIndex = 6

at java.util.SubList.(AbstractList.java:602)

at java.util.RandomAccessSubList.(AbstractList.java:758)

at java.util.AbstractList.subList(AbstractList.java:468)

at managedbeans.GeradorDeApostasBean.geraApostas(GeradorDeApostasBean.java:25)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

at com.sun.el.parser.AstValue.invoke(AstValue.java:234)

at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)

at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:98)

at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)

 32 more

|#]
Criado 7 de janeiro de 2011
Ultima resposta 15 de ago. de 2011
Respostas 4
Participantes 4