Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to invoke virtual method '....RetrievalAdapter.getModelClass()' on a null object reference #1730

Open
jchristof opened this issue Dec 27, 2021 · 2 comments

Comments

@jchristof
Copy link

DBFlow Version: 4.2.4

Bug or Feature Request: Attempt to invoke virtual method '....RetrievalAdapter.getModelClass()' on a null object reference

Description:
I'm getting the exception occasionally in production. Not sure why this fails only occasionally. Anyone have an idea?

Fatal Exception: java.lang.NullPointerException
Attempt to invoke virtual method 'java.lang.Class com.raizlabs.android.dbflow.structure.RetrievalAdapter.getModelClass()' on a null object reference

com.raizlabs.android.dbflow.sql.saveable.ModelSaver.getWritableDatabase (ModelSaver.java:169)
com.raizlabs.android.dbflow.sql.saveable.ModelSaver.save (ModelSaver.java:29)
com.raizlabs.android.dbflow.structure.ModelAdapter.save (ModelAdapter.java:186)
com.raizlabs.android.dbflow.structure.BaseModel.save (BaseModel.java:62)
@jschools
Copy link

I am also getting this error occasionally. I think it is a race condition in ModelAdapter.getModelSaver(). The ModelAdapter lazily initializes an instance of ModelSaver without synchronization here:


If multiple threads call getModelSaver() concurrently, it is possible for one thread to get (and attempt to use) the modelSaver instance before setModelAdapter(this) has been called on it, leading to the NPE.

This seems to have been (kind of) fixed in 5.0.0-alpha2. It could result in multiple instances of ModelSaver being used, which would solve this crash but introduce the possibility of synchronization errors, since ModelSaver has several @Synchronized functions:


It is not trivial to upgrade to the new library, and it is also risky to upgrade to a new major "dot zero alpha" version.

As a workaround, you could call .getModelSaver() on each ModelAdapter immediately after initializing DbFlow, as long as you can ensure no other threads are calling save() while this is happening.

@loquitowen
Copy link

loquitowen commented Mar 14, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants