2

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.

1
  • 2
    You are going to laugh at yourself, I am sure. :D Commented Aug 20, 2015 at 13:38

2 Answers 2

6

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];
Sign up to request clarification or add additional context in comments.

2 Comments

Omg, really? Silly error. Useful and fast answer, thank you hiergiltdiestfu!
@JoseLopez You're very welcome :) I especially liked how you amended your code sample to reflect what you're seeing.
3

Funny reason: You are using same memory location for all the string array.

Why you are getting values of strComentarios in every array: Because a new memory location is assigned to it and you are using same memory location for other array. So whatever is updated in strComentarios, all other array get that value.

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

Split it like this

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

2 Comments

Hey Rohit5k2, thank you so much! Your answer was exactly the same as hiergiltdiestfu's, so to choose an accepted answer I need to follow the "first reply" principle. But yours is useful as well, thank you for your time :)
No problem. We are here to help. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.