Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – Why I’m Getting Null Object Reference Error?

Posted on November 11, 2022 By

Solution 1 :

This line

Users users = dataSnapshot.getValue(Users.class);

returns null.
So your users object is null and the follow-up-line

mName.setText("Name: "+ users.getFullName());

causes the NullReferenceException

The solution is

Users users = dataSnapshot.getValue(Users.class);
if (users != null) {
    mName.setText("Name: "+ users.getFullName());
    mUsername.setText("Username: "+users.getmUserName());
    mEmail.setText("Email: "+users.getEmail());
    phoneNo.setText("Phone No: "+users.getMobileNumber());
} else {
    // write a log, write something else to the text boxes...
}

Maybe you need to check whether dataSnapshot even contains data about users? What else could have changed?

To me it looks like you blindly accept that callback without checks what has changed. Maybe this even triggers when data is deleted? Maybe there is other data contained in the snapshot, but not Users?

Problem :

This question already has answers here:

What is a NullPointerException, and how do I fix it?

(12 answers)
Closed 2 years ago.

i’m trying to retrieve data from firebase database i have finished the code with everything except for 1 minor error which every time i click to go to profile activity i’m getting this error:
“Attempt to invoke virtual method ‘java.lang.String com.example.logindesign.Users.getFullName()’ on a null object reference”

Here’s is my profile activity code. i’m getting the error when i’m calling mName.setText(“Name: “+ users.getFullName());

package com.example.logindesign;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

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

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;
import com.squareup.picasso.Picasso;

import java.util.Objects;

public class Profile extends AppCompatActivity {

    FirebaseAuth firebaseAuth;
    FirebaseUser user;
    FirebaseDatabase firebaseDatabase;
    DatabaseReference databaseReference;
    ImageView imageView;
    TextView mUsername, phoneNo, mEmail,  mName;

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


        firebaseAuth = FirebaseAuth.getInstance();
        user = firebaseAuth.getCurrentUser();
        firebaseDatabase = FirebaseDatabase.getInstance();
        databaseReference = firebaseDatabase.getReference(firebaseAuth.getUid());

        imageView = findViewById(R.id.imageView2);
        mUsername = findViewById(R.id.textView);
        phoneNo = findViewById(R.id.textView1);
        mEmail = findViewById(R.id.textView2);
        mName = findViewById(R.id.textView3);


        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                Users users = dataSnapshot.getValue(Users.class);

                mName.setText("Name: "+ users.getFullName());
                mUsername.setText("Username: "+users.getmUserName());
                mEmail.setText("Email: "+users.getEmail());
                phoneNo.setText("Phone No: "+users.getMobileNumber());


            }

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

            }
        });




    }
}

and this is my Users Activity Code

package com.example.logindesign;

public class Users {

    public String fullName;
    public String mUserName;
    public String email;
    public String mobileNumber;

    public Users(){

    }

    public Users(String fullName, String mUserName, String email, String mobileNumber) {
        this.fullName = fullName;
        this.mUserName = mUserName;
        this.email = email;
        this.mobileNumber = mobileNumber;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getmUserName() {
        return mUserName;
    }

    public void setmUserName(String mUserName) {
        this.mUserName = mUserName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getMobileNumber() {
        return mobileNumber;
    }

    public void setMobileNumber(String mobileNumber) {
        this.mobileNumber = mobileNumber;
    }
}

any idea on what’s causing this error?

READ  [FIXED] java - Android Studio - listing reviews from Firebase
Powered by Inline Related Posts

Comments

Comment posted by Wisam Atil

Ok now the error is gone, Thank you for that. But the data from database is not showing now.

Comment posted by Grisgram

you need to find out, why

Android Tags:android, nullpointerexception

Post navigation

Previous Post: [FIXED] Is it possible to switch from Start state to End state in android MotionLayout using Java/Kotlin?
Next Post: [FIXED] android – PHP / SQL: cannot save image at server but successful save image link at SQL database

Related Posts

[FIXED] android – “No Chart Data Available” MPAndroidChart in Kotlin Android
[FIXED] java – where does content://media/external/audio/albumart come from and is this the same across all devices? Android
[FIXED] android – RecyclerView and cardView onclick Android
[FIXED] android – Flutter app errors will trying to connect with Firebase and Auth Android
[FIXED] android – com.microsoft.identity.client.exception.MsalClientException: Unable to perform cloud discovery Android
[FIXED] android – how to change modal sheet top edges to circular shape not working Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What is a good substitute for condensed milk?
  • ¿Como el cuerpo te avisa cuando te va a dar un infarto?
  • What is the luxury brand of Jaguar?
  • Who is Big poppa baseball player?
  • What material are foam runners?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme