0

I developed an android app using Firebase real time database and some other Firebase services, in the database I have a reference for users, where I store the needed information about the users like this:

enter image description here

I make sure when storing to the database that the user and all of it's fields are initialized and not null or undefined.

All worked fine before trying to enable offline capabilities following this documentation, now I added this line as explained in the documentations in the main activity:

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

and the app works fine offline most of the time, but sometimes, the app crashes with NullPointerException when getting the users list, I check the database and I find a user with only two fields; instanceId, and Id, the rest of the fields are not there. I don't know why is this happening but surely, supporting offline capabilities is the problem.

I'd really appreciate any help I've been looking for days for a solution. Thank you!

  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) –  Jun 28 '18 at 09:42
  • @Ibrahim no, I know what is NullPointerException and when I write to the database I create an object with all fields initialized and I store it like this: `myDataBase.child("users").child(currentUser.getUid()).setValue(user);` user is not null and all it's fields are initialized and not null... – Mohamed Taher Jun 28 '18 at 09:51

1 Answers1

1

I hope this will work for you.

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

Put above line in your application class onCreate() method. if not created, Created like this

import android.app.Application;

public class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        FirebaseDatabase.getInstance().setPersistenceEnabled(true);
    }
}

And call this class in Manifest like this,

 <application
        android:name=".App"
        ......
             />
GParekar
  • 1,209
  • 1
  • 8
  • 15
  • 1
    I will try this thank you, I'd appreciate a short explanation about what you did and why you did that and how do you think it solves the problem? – Mohamed Taher Jun 28 '18 at 09:52
  • App class is instantiated before any other class when the process for your application/package is created. So you will get FirebaseInstance first before do any task with firebase.I hope this will work for you. – GParekar Jun 28 '18 at 10:17