0

I am new to Android Programming and i am following some tutorial and the tutorial is 1 year ago. i am stuck with this error i think the problem is related to the database but i dont know anything yet about android.

PID: 23165
    java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.DatabaseReference com.google.firebase.database.DatabaseReference.child(java.lang.String)' on a null object reference
        at com.example.fooddeliver.MainActivity.checkUserFromFirebase(MainActivity.java:101)
        at com.example.fooddeliver.MainActivity.lambda$init$0$MainActivity(MainActivity.java:83)
        at com.example.fooddeliver.-$$Lambda$MainActivity$9ehXip3aW7uFlecdkbtLT68AicY.onAuthStateChanged(Unknown Source:2)
        at com.google.firebase.auth.zzj.run(com.google.firebase:firebase-auth@@19.3.1:2)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at com.google.android.gms.internal.firebase_auth.zzj.dispatchMessage(com.google.firebase:firebase-auth@@19.3.1:6)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
I/Process: Sending signal. PID: 23165 SIG: 9

my code is this.

Update i put my init because i dont know how to initialize userRef sorry for the newbie post

    private void init() {
        providers = Arrays.asList(new AuthUI.IdpConfig.PhoneBuilder().build());

        firebaseAuth = FirebaseAuth.getInstance();

        dialog = new SpotsDialog.Builder().setCancelable(false).setContext(this).build();
        cloudFunctions = RetrofitCloudClient.getInstance().create(ICloudFunctions.class);
        listener = firebaseAuth -> {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if(user != null)
            {
                checkUserFromFirebase(user);
            }
            else
            {
                phoneLogin();
            }
        };
    }

private void checkUserFromFirebase(FirebaseUser user) {
        dialog.show();
        userRef.child(user.getUid())
                .addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                        if(snapshot.exists())
                        {
                            Toast.makeText(MainActivity.this, "You are Already Registered", Toast.LENGTH_SHORT).show();

                            UserModel userModel = snapshot.getValue(UserModel.class);

                            goToHomeActivity(userModel);
                        }
                        else
                        {
                            showRegisterDialog(user);
                        }

                        dialog.dismiss();
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {
                        dialog.dismiss();
                        Toast.makeText(MainActivity.this, ""+error.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
    }
George Uy
  • 11
  • 1
  • Java is to Javascript as Pain is to Painting, or Ham is to Hamster. They are completely different. It is highly recommended that aspiring coders try to learn the name of the language they're attempting to write code in. When you post a question, please tag it appropriately - this lets those with knowledge of the language you need help with to see your question. – CertainPerformance Dec 05 '20 at 05:32
  • Im sorry as i am new to the community – George Uy Dec 05 '20 at 05:34
  • It looks like `userRef` is not initialized. It's impossible to say from the code you shared why that is, but you'll want to make sure it has a value before you call `checkUserFromFirebase`. – Frank van Puffelen Dec 05 '20 at 05:35
  • Thank you @FrankvanPuffelen i think i know where i will dig now – George Uy Dec 05 '20 at 05:37
  • 1
    I added the userRef Function userRef = FirebaseDatabase.getInstance().getReference(Common.USER_REFERENCES); Thank you so much @FrankvanPuffelen – George Uy Dec 05 '20 at 05:45
  • Good to hear you got it working @GeorgeUy – Frank van Puffelen Dec 05 '20 at 15:18

0 Answers0