question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Model reference becomes null after adding to a different model relation

See original GitHub issue

Summary

With the code below it can be seen that I have an Account : Program relation as well as Account : []Account. The Program can be accessed through the Account until a new relation is added, after that I the account.program is immediately null. Using stetho it can be seen that the program is now a null value.

Account
class Account {
    public Program program;
    public RealmList<Account> following;
}
Follow account
// ...
realm.executeTransaction(new Realm.Transaction() {
    @Override
    public void execute(Realm realm) {
        Account currentUser = realm.where(Account.class)
                .equalTo(Account.ATTR_KEY, Session.getAccountKey(context))
                .findFirst();

        Account a = resp.body();          // account's program is not null here
        Log.d("program", currentUser.program.name);   // shows the program name
        currentUser.following.add(a);   // results in Account -> Program disconnect
        Log.d("program", currentUser.program.name);   // null pointer exception
    }
});
// ...

Version of Realm and tooling

Realm version(s): 2.0.2

Android Studio version: ? 2.2.1

Which Android version and device: ? 4.2.1 Galaxy Nexus

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
chrisolsencommented, Oct 17, 2016

You are right in regards to the account payload from resp.body() not containing the program and then deleting it.

Performing a realm lookup for the account, then adding that account reference works.

Sorry for the post here, I thought it was a bug 😦

0reactions
Zhuindencommented, Oct 17, 2016

Well considering you overrode the original currentUser as the proxy pointed to the same object, it’s no wonder the error looked so arcane 🙂

Read more comments on GitHub >

github_iconTop Results From Across the Web

Laravel Eloquent 'with' returns null, but referencing the ...
I'd love to understand why the filtered relationship works fine when calling the relationship method but not using 'with'. – Andrew. Apr 14, ......
Read more >
Working with nullable reference types - EF Core
C# 8 introduced a new feature called nullable reference types (NRT), allowing reference types to be annotated, indicating whether it is ...
Read more >
Detect if `belongsTo` is null, and toggle associations - Ember.JS
I am missing something about how to determine if a belongsTo relationship on a model is set. And how I can toggle that...
Read more >
Data model (Reference) - Prisma
Learn about the concepts for building your data model with Prisma: Models, scalar types, enums, attributes, functions, IDs, default values and more.
Read more >
attempt to de-reference null object when using maps to build ...
So adjacentGroup must be null. That variable is set from the result of groupsMap.get(o.PDC__c) . The issue you're running into is that ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found