In a activy I have written code that works correctly.
But now I have added a method to this activity with the following code:
private void obtenerDatosReuniones(){
try {
int j=0;
String aux = jsonReuniones.getString("nombres");
String aux2 = null;
aux2 = aux.replace("[", "");
aux2= aux2.replace("]", "");
String [] campos = aux2.split(",");
while(j<campos.length){
nombres_reuniones.add(campos[j]);
}
the type of nombres_reunones is ArrayList
When I run the application appears out the following error on line nombres_reuniones.add (campos [j]):
What am I doing wrong?
Thanks!
Look at your loop:
How do you anticipate that ever finishing? You don’t modify
j. Given that you don’t make any change tojafter declaring it and assigning it a value of0right at the start, it would be much clearer as:Or better:
Or even simpler:
Additionally, your earlier code can be simpler. Look at this:
Why bother assigning
aux2an initial value ofnullwhich you then immediately overwrite? Additionally, you can easily chain the method calls. It would be neater as:And in fact, you can just chain the whole of the string manipulation together from start to finish:
(I’d stop there, rather than inlining even that expression…)