-4

I have designed an app which retrieves a user's data from the firebase server. But the java code is unable to retrieve the user's details from the realtime database.

The error that is raised is that the below lines receive a NullPointer.

User userDashboard = dataSnapshot.getValue(User.class);
USERNAME.setText(userDashboard.getName().toString().trim()); 
PASSWORD.setText(userDashboard.getPassword().toString().trim());
EMAIL.setText(userDashboard.getEmail().toString().trim());

Below is the java code.

package com.example.loginuiapp;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

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

import com.google.firebase.auth.FirebaseAuth;
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.ValueEventListener;

public class UserDashboard extends AppCompatActivity {

    Button btnLogout;

    private TextView USERNAME, PASSWORD, EMAIL;


    private FirebaseAuth firebaseAuth;
    private FirebaseDatabase firebaseDatabase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // MAKING THE APP FULLSCREEN. KINDLY DO NOT DELETE THE BELOW LINES WITHOUT PRIOR KNOWLEDGE
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_user_dashboard);

        USERNAME = findViewById(R.id.display_username);
        PASSWORD = findViewById(R.id.display_usr_password);
        EMAIL = findViewById(R.id.display_usr_email);


        firebaseAuth = FirebaseAuth.getInstance();
        firebaseDatabase = FirebaseDatabase.getInstance();

        DatabaseReference databaseReference = firebaseDatabase
                .getReference(firebaseAuth.getUid());

        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {/*
                User userDashboard = dataSnapshot.getValue(User.class);
                USERNAME.setText(userDashboard.getName().toString().trim());
                PASSWORD.setText(userDashboard.getPassword().toString().trim());
                EMAIL.setText(userDashboard.getEmail().toString().trim());*/
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(UserDashboard.this, "Database Error", Toast.LENGTH_LONG).show();
            }
        });


        // Handling the Logout action when the logout button is clicked.
        btnLogout = findViewById(R.id.logout_button);
        btnLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FirebaseAuth.getInstance().signOut();
                // Read this thread to know what's happening
                // https://stackoverflow.com/questions/53334017/back-button-will-bring-to-home-page-after-firebase-logout-on-app
                Intent intent = new Intent(UserDashboard.this, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_CLEAR_TOP
                        | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
                finish();
                Toast.makeText(UserDashboard.this, "Successfully Logged Out!", Toast.LENGTH_LONG).show();


            }
        });
    }
}

Thank you for your help!

Sunil Sunny
  • 3,949
  • 4
  • 23
  • 53
Ankur Paul
  • 9
  • 1
  • 5
  • Can you update your question to show the JSON that you stored for the user (as text, no screenshots please)? You can get this by clicking the "Export JSON" link in the overflow menu (⠇) on your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). – Frank van Puffelen Nov 22 '19 at 02:46

1 Answers1

0

In your Database Reference make sure you are having the correct path to the data your are retrieving, like

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("your/path/to/data");
ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            //making sure snapshot consists some data 
            if (dataSnapshot.exists()) {
                //do your stuff
                User userDashboard = dataSnapshot.getValue(User.class);
                USERNAME.setText(userDashboard.getName().toString().trim());
                PASSWORD.setText(userDashboard.getPassword().toString().trim());
                EMAIL.setText(userDashboard.getEmail().toString().trim());
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Toast.makeText(UserDashboard.this, "Database Error", Toast.LENGTH_LONG).show();
        }
    });
Praveen G
  • 794
  • 1
  • 13
  • 24
  • This code has solved my issue. Ones who have downvoted my question, please know that I am new to firebase cloud and android. I am a new learner, not everybody know everything from the preliminary stage. Thank You! – Ankur Paul Nov 26 '19 at 20:42