Strange behavior when assigning a String variable to a string

advertisements

I don't really understand why this is happening. This is, by far, the weirdest error I've never come across.

The thing is I'm retrieving some info from a Server using Retrofit, and for that, I needed to create a POJO called POJO_PATIENTINFO. Inside this pojo, I have a BigDataset_PatientInfo variable called data. Inside this variable, I am storing a List<Dataset_RegVar> called registro_variable, inside of which I have these strings:

  • registro_var_id
  • registro_var_fecha
  • registro_var_descripcion
  • registro_var_variable
  • registro_var_medida
  • registro_var_comentario

No problems with that. The idea is that I want to recover these Strings from another Fragment.class, easy task I thought.

This is my piece of code for doing such an easy task:

int size = POJOS.getPojo_patientInfo().data.registro_variable.size();
int i = 0;
strVariable = strId = strFecha = strMedida = strDescripcion = strComentarios = new String[size];

for (POJO_PATIENTINFO.Dataset_RegVar dataset : POJOS.getPojo_patientInfo().data.registro_variable) {
    strVariable[i] = dataset.registro_var_variable;
    strId[i] = dataset.registro_var_id;
    strFecha[i] = dataset.registro_var_fecha;
    strMedida[i] = dataset.registro_var_medida;
    strDescripcion[i] = dataset.registro_var_descripcion;
    strComentarios[i] = dataset.registro_var_comentario;
    i++;
}

After the first iteration, just before the "i++;" line executes, the String arrays are all storing exactly the same value, that is, the same value as dataset.registro_var_comentario. In a nutshell, the aforementioned piece of code is execute as if I was writing:

int size = POJOS.getPojo_patientInfo().data.registro_variable.size();
int i = 0;
strVariable = strId = strFecha = strMedida = strDescripcion = strComentarios = new String[size];

for (POJO_PATIENTINFO.Dataset_RegVar dataset : POJOS.getPojo_patientInfo().data.registro_variable) {
    strVariable[i] = dataset.registro_var_comentario;
    strId[i] = dataset.registro_var_comentario;
    strFecha[i] = dataset.registro_var_comentario;
    strMedida[i] = dataset.registro_var_comentario;
    strDescripcion[i] = dataset.registro_var_comentario;
    strComentarios[i] = dataset.registro_var_comentario;
    i++;
}

Obviously, the first thing that came to my mind is that maybe all of these strings were holding the same value. No, they are not. I know this because I did some debugging work on my own. Let me paste some results (note that these images are for i=3): http://postimg.org/gallery/2gf1lj7qa/

I will provide more details if you think they are necessary. Thanks.


You are assigning one and the same String array to all those variables. So the last write to any of these variables overwrites the corresponding index in all the other variables aswell. And this happens to be this assignment:

strComentarios[i] = dataset.registro_var_comentario;

So instead of

strVariable = strId = strFecha = strMedida = strDescripcion = strComentarios = new String[size];

you need to write

strVariable = new String[size];
strId = new String[size];
strFecha = new String[size];
strMedida = new String[size];
strDescripcion = new String[size];
strComentarios = new String[size];