0

I'm trying to create a simple login module using the Firebase Realtime Database. But then I keep on getting this error:

java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.auth.FirebaseUser com.google.firebase.auth.FirebaseAuth.getCurrentUser()' on a null object reference.

Here is the code

package com.example.thecruds;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;

public class LoginActivity extends AppCompatActivity {

    private Button l_login, l_register;
    private EditText l_username, l_password;

    FirebaseAuth auth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        l_login = findViewById(R.id.l_login);
        l_register = findViewById(R.id.l_register);
        l_username = findViewById(R.id.l_usermail);
        l_password = findViewById(R.id.l_password);

        l_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Validation
                String password = l_password.getText().toString().trim();
                String username = l_username.getText().toString().trim();

                DatabaseReference reference = FirebaseDatabase.getInstance().getReference("User");
                FirebaseUser user = auth.getCurrentUser();
                String uid =  user.getUid();

                Query checkUser = reference.orderByChild(uid).equalTo(password);

                if (l_username.length() == 0) {
                    l_username.setError("This field must not be empty");
                } else if (l_password.length() == 0) {
                    l_password.setError("This field must not be empty");
                } else {
                    checkUser.addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot snapshot) {
                            if (snapshot.exists()){
                                String dbPassword = snapshot.child(uid).child("password").getValue(String.class);
                                String dbUsername = snapshot.child(uid).child("username").getValue(String.class);
                                if (dbPassword == password && dbUsername == username){
                                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                    startActivity(intent);
                                    finish();
                                }
                            }
                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError error) {

                        }
                    });
                }
            }
        });

        l_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
    }
}

I also updated the rules in Realtime Database

{
  "rules": {
    ".read": true,
    ".write": true,
    "User": {
       "$uid": {
         ".read": "$uid === auth.uid",
         ".write": "$uid === auth.uid"
       }
     }
   }
}
Frank van Puffelen
  • 499,950
  • 69
  • 739
  • 734
  • You have not initialized `auth` anywhere in the code, that's why `auth.getCurrentUser()` gives null error – Nitish Oct 14 '21 at 12:15

0 Answers0