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

Asked

Viewed 357 times

0

I’m having an error to save my package’s vigencias, I’m passing the array position. which may 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": []
            }
        ]
    }

1 answer

2


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

Browser other questions tagged

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