1

I am developing and Android app and want to save to a local SQLite Database and then want it to sync with Firebase, I have gotten the Firebase sync working I am only having the problem saving to the local database, the app crashes when I click save however the data gets stored in firebase but not in the local SQLite. I have included my code below if anyone can assist.

 buttonSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Creating firebase object
            Firebase ref = new Firebase(Config.FIREBASE_URL);
            ref.keepSynced(true);

            //Getting values to store
            String name = editTextName.getText().toString().trim();
            String address = editTextAddress.getText().toString().trim();
            String venue = editVenue.getText().toString().trim();
            String DateTime = editDateTime.getText().toString().trim();

            //Creating Person object
            final MeetingUser person = new MeetingUser();

            //Adding values
            person.setName(name);
            person.setAddress(address);
            person.setVenue(venue);
            person.setDateTime(DateTime);

            //Storing values to firebase
            ref.push().setValue(person);

            //Write to local SQL database
            boolean isInserted = DB.SaveUserMeetings(editTextName.getText().toString(),
                    editDateTime.getText().toString(),
                    editVenue.getText().toString(),
                    editTextAddress.getText().toString());
            if (isInserted == true)
                Toast.makeText(Capture_Meetings.this, "Data Not Saved", Toast.LENGTH_SHORT).show();
            else
                Toast.makeText(Capture_Meetings.this, "Data Saved", Toast.LENGTH_SHORT).show();


            //Value event listener for realtime data update
            ref.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot snapshot) {
                    for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                        //Getting the data from snapshot
                        MeetingUser person = postSnapshot.getValue(MeetingUser.class);

                        //Adding it to a string
                        String string = "Name of School: " + person.getName() + "\n Date:" + person.getDateTime() + "\n Venue:" + person.getVenue() + "\nMeeting Notes: " + person.getAddress() + "\n\n";

                        //Displaying it on textview
                        textViewPersons.setText(string);
                    }
                }

                @Override
                public void onCancelled(FirebaseError firebaseError) {
                    System.out.println("The read failed: " + firebaseError.getMessage());
                }
            });

        }
    });
}

My Error log

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.pooveshin.saica_sgb, PID: 10770
              java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.example.pooveshin.saica_sgb.DatabaseHelper.SaveUserMeetings(java.lang.String, java.lang.String, java.lang.String, java.lang.String)' on a null object reference
                  at com.example.pooveshin.saica_sgb.Capture_Meetings$3.onClick(Capture_Meetings.java:114)
                  at android.view.View.performClick(View.java:4780)
                  at android.view.View$PerformClick.run(View.java:19866)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:135)
                  at android.app.ActivityThread.main(ActivityThread.java:5254)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Pooveshin
  • 201
  • 4
  • 13
  • What problem do you have? Without a clear question, it is unlikely that somebody will help. – Frank van Puffelen Dec 05 '16 at 09:14
  • @FrankvanPuffelen when I try to save the data gets written to firebase however it crashes when it tries to save to the local database. – Pooveshin Dec 05 '16 at 09:19
  • It looks like you're trying to invoke `SaveUserMeetings` on a `null` object: `DB.SaveUserMeetings(...)`. See http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Frank van Puffelen Dec 05 '16 at 09:22

1 Answers1

1

Have you considered using Firebase offline capabilities (https://firebase.google.com/docs/database/android/offline-capabilities) ...enabled by calling following

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

John O'Reilly
  • 8,536
  • 4
  • 31
  • 49
  • Its a requirement from the client to have a local Database – Pooveshin Dec 05 '16 at 09:23
  • I'm curious why that's needed? To me this is one of the really nice capabilities of Firebase...once I retrieve data it's stored locally and any subsequent queries I make for that data don't need to go over the network. – John O'Reilly Dec 05 '16 at 09:25