2

I'm trying to get a value I have saved in Realm, but I don't know how to do it.. the value is always null.. but when I'm debugging the code, the value appear as you can see in the image below:

Note: BookMarkActive & idBookMark has the proper value in the selected line but below does not appear anything.

enter image description here

How can I get the value?

Take a look the same code as in the image:

public String UpdateBookMarks(String id){
    Realm realm = Realm.getInstance(_context);
    RealmQuery<Bookmark> query = realm.where(Bookmark.class);
    query.equalTo("IdBookMark", id);
    Bookmark stationDetails = query.findFirst();
    if(stationDetails.getBookMarkActive() == "true"){
        stationDetails.setIdBookMark("false");
        return "Station has been removed from your bookmark list.";
    }else if (stationDetails.getBookMarkActive() == "false"){
        stationDetails.setIdBookMark("true");
        return "Station has been added to your bookmark list.";
    }
    return "Error";
}

BookMark class:

@RealmClass
public class Bookmark extends RealmObject {
    private java.lang.String IdBookMark;
    private String BookMarkActive;

    public String getIdBookMark() {
        return IdBookMark;
    }

    public void setIdBookMark(String idBookMark) {
        IdBookMark = idBookMark;
    }

    public String getBookMarkActive() {
        return BookMarkActive;
    }

    public void setBookMarkActive(String bookMarkActive) {
        BookMarkActive = bookMarkActive;
    }
}

Note: Realm version 'io.realm:realm-android:0.87.2'

gon250
  • 3,405
  • 6
  • 44
  • 75
  • So it crashes when you are calling `stationDetails.getBookMarkActive()` because `stationDetails` is null? – cylon Jan 15 '16 at 18:19
  • Yep...but the value is there as you can se in the selected line in the image but I get null ..do u know what I mean? @ – gon250 Jan 15 '16 at 19:00
  • By the way, `stationDetails.getBookMarkActive()` returns a string? If so, you should compare with `.equals("true")`, otherwise compare it to `true` and not to `"true"`. Same goes to the else branch. – cylon Jan 15 '16 at 19:19
  • I have updated the code so you can see my class(`BookMarkActive` is a String). I get null and I dont know why, how can I get it? @cylon – gon250 Jan 15 '16 at 20:19
  • 2
    Note that you cannot trust the debugging view for RealmObjects as they are just proxies for the underlying data. You can see read about it here: https://realm.io/docs/java/latest/#debugging – Christian Melchior Jan 15 '16 at 20:40
  • Thanks @christian-melchior you are right I didn't see it. If you want to answer the question I'll validate your answer :). – gon250 Jan 16 '16 at 11:22

1 Answers1

5

You cannot trust the debugging view in Android Studio for RealmObjects managed by Realm. They are just proxy objects for the true underlying data that are stored in native memory. This means that that fields will only show the default value for the field: 0 for int, null for String and references.

You can read more about it here: http://realm.io/docs/java/latest/#debugging

Christian Melchior
  • 19,978
  • 5
  • 62
  • 53
  • 1
    Just a side-note: you can get the actual values of the variables, by using the "Watches" window and calling either a variable on your object directly or using the mandatory getters for the variables, while debugging. – Darwind Jan 17 '16 at 23:40