I'm learning to program on Java and I've been really frustrated because I can't find a way to convert a parse.com object that I download to a costume class.
What I'm doing:
public void downloadInformation() {
usuario = ParseUser.getCurrentUser();
ParseQuery<ParseObject> query = ParseQuery.getQuery("Glucosa");
query.whereEqualTo("usuario", usuario);
query.orderByDescending("createdAt");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null && objects.size() > 0) {
Log.d("score", "Objects Retrived");
int i = 0;
for (ParseObject object : objects) {
dataArray = new MyData[objects.size()];
dataArray[i] = new MyData(object.getInt("glucosa"), object.getInt("insulina"), object.getDate("fecha"), object.getInt("Alimentos"), object.getString("Comentarios"));
i++;
}
for (int j=0; j <= dataArray.length; j++){
Log.i("Working?", dataArray[j].comentarios);
}
} else {
Log.d("score", "error");
}
}
});
}
The code of MyData is:
public class MyData {
Integer gluc;
Integer insulinaV;
Date fec;
Integer alimento;
String comentarios;
public MyData(Integer gluc, Integer insulinaV, Date fec, Integer alimento, String comentarios) {
this.gluc = gluc;
this.insulinaV = insulinaV;
this.fec = fec;
this.alimento = alimento;
this.comentarios = comentarios;
}
public Integer getGluc() {
return gluc;
}
public Integer getInsulinaV() {
return insulinaV;
}
public Date getFec() {
return fec;
}
public Integer getAlimento() {
return alimento;
}
public String getComentarios() {
return comentarios;
}
}
But it doesn't matter what I try to recover, dataArray is alway empty. Even if I try to print dataArray.length it crashes.
What am I doing wrong?, What is the correct way to store an object like this? One almost of topic question, is there an easiest way to do this? in SWIFT for example I use a struct with objects.map, but I haven't find a method that can do this.
Thanks!!
Edit: There are 22 elements in objects, but the array dataArray is always null.
The error message I get is:
java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.parse.starter.MyData.comentarios' on a null object reference
The null object reference comes even if I simply try to get the size of the dataArray.