0

I'm learning to program in Java for Android Studio. I'm working with a Parse.com query downloading information. I store the information inside an array of a costume object called MyData. When I'm storing the information I can log the content of the array and it has the correct info. But latter when I try to use the same array, if I use the .length function it says it's null. And if I try to retrieve any of the information, it's empty.

This I my object:

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;
}

}

So, to retrieve the information I use array[I].getWhatever(), this is how I store the information:

public void downloadInformation() {
user = ParseUser.getCurrentUser();

  ParseQuery<ParseObject> query = ParseQuery.getQuery("Glucosa");
  query.whereEqualTo("usuario", user);
  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");
              Log.d("size", String.valueOf(objects.size()));
              int i = 0;
              indexsize = 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"));

                String alimentosexiste = dataArray[i].getAlimento().toString();
                Log.i("Is Empty or Not=", alimentosexiste);
                indexsize = indexsize+1;
                i++;

              }

          } else {
              Log.d("failed", "error");
          }
      }
  });

}

In my logcat I'm getting "Score! Objects retrieved" and "Size: 22", also I get a list with all 22 elements of the "Is Empty or Not" Log. So far so good.

Then, In my attempt to move from this activity to another, I try to save the dataArray with:

public void saveInformation() {

int j = indexsize;

Log.i("size of index?", String.valueOf(indexsize));

for (int i=0; i<=j; i++) {
  Log.i("index", String.valueOf(i));
  alimentosVal = dataArray[i].getAlimento();
  comentariosVal = dataArray[i].getComentarios();
  glucVal = dataArray[i].getGluc();
  insulinaVal = dataArray[i].getInsulinaV();
  fecVal = dataArray[i].getFec();


}

SQLiteDatabase myGlucosebase = this.openOrCreateDatabase("GlucoseEvents", MODE_PRIVATE, null);
myGlucosebase.execSQL("CREATE TABLE IF NOT EXISTS glucoseevents (alimentos INT(2), comentarios VARCHAR, gluc INT(4), insulinv INT(4), fec  DATETIME)");
myGlucosebase.execSQL("INSERT INTO glucoseevents (alimentos, comentarios, gluc, insulinv, fec) VALUES (alimentosVal, comentariosVal, glucVal, insulinaVal, fecVal) ");

}

And even do I printed before the content of the array with index [0] (so I'm sure the information got stored in the array), I get the following error:

Attempt to invoke virtual method 'java.lang.Integer com.parse.starter.MyData.getAlimento()' on a null object reference

I've seen that the problem is that I'm pointing to an empty element, but it was working before, how can I do this?

(Data array is declared at the beginning, below the class name as: MyData[] dataArray;)

Thanks!

2
  • 2
    Move dataArray = new MyData[objects.size()]; to before your loop, otherwise you are recreating every iteration of the loop Aug 19, 2019 at 0:56
  • @ScaryWombat This solved the problem, thanks a lot!
    – Enrique
    Aug 19, 2019 at 1:01

2 Answers 2

0

dataArray = new MyData[objects.size()]; should be outside the for loop

-3

Your class MyData does not have a "dataArray". At least not in the example code you give above.

2
  • Well, it is not in the code he posted, so it is true :D Aug 19, 2019 at 1:14
  • Yes, and I have no idea what YOU are looking at. The code on this page has no declaration for that array. No idea why you want to argue about it and why you downvote me for no reason. To bad I can not add a screenshot :D Aug 19, 2019 at 1:31

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.