java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2

0

I have an error to save the validity of my package, I am passing the position of the array. What could be wrong?

  int position = 0;
  for (Pacote pacote: pacoteList) {
     Vigencia vigencia = new Vigencia();
     if (pacote.getVigencias().size() != 0) {
        Log.d("Vigencia", pacote.getVigencias().get(position).getMeses());

        vigencia.setMeses( pacote.getVigencias().get(pacoteList.indexOf(pacote)).getMeses());
        vigencia.setValorDescontoPromocional( pacote.getVigencias().get(pacoteList.indexOf(pacote)).getValorDescontoPromocional());
        vigenciaDao.inserirVigencia(db, vigencia, pacote.getCodigo());
     }

     position++;

  }

Json

  "Pacotes": [
            {
                "Codigo": "1",
                "Descricao": "Pacote teste",
                "SimboloMoeda": "US$",
                "Adesao": "18.00",
                "ValorContrato": "150.00",
                "DescontoPacote": "-100.00",
                "ValorDescontoPromocional": "0.00",
                "Vigencias": [
                    {
                        "meses": "12",
                        "ValorDescontoPromocional": "5.00"
                    },
                    {
                        "meses": "24",
                        "ValorDescontoPromocional": "5.00"
                    }
                ]
            },
            {
                "Codigo": "2",
                "Descricao": "teste webservice",
                "SimboloMoeda": "R$",
                "Adesao": "130.00",
                "ValorContrato": "170.00",
                "DescontoPacote": "50.00",
                "ValorDescontoPromocional": "0.00",
                "Vigencias": []
            },
            {
                "Codigo": "3",
                "Descricao": "Pacote Salesforce WS",
                "SimboloMoeda": "R$",
                "Adesao": "0.00",
                "ValorContrato": "499.90",
                "DescontoPacote": "5.00",
                "ValorDescontoPromocional": "0.00",
                "Vigencias": [
                    {
                        "meses": "12",
                        "ValorDescontoPromocional": "15.00"
                    },
                    {
                        "meses": "24",
                        "ValorDescontoPromocional": "35.00"
                    }
                ]
            },
            {
                "Codigo": "4",
                "Descricao": "SalesForce 002",
                "SimboloMoeda": "R$",
                "Adesao": "0.00",
                "ValorContrato": "180.00",
                "DescontoPacote": "0.00",
                "ValorDescontoPromocional": "0.00",
                "Vigencias": []
            }
        ]
    }
    
asked by anonymous 08.12.2017 / 13:11

1 answer

2

You are increasing the variable position each time it iterates through a package. In the third package this variable has value 2 (since it started in 0 ), only that the array of vigencias of the third package only has the indices 0 (first item) and 1 (second item), consequently you try to access an index that does not exist.

    
08.12.2017 / 13:23