Answer the question
In order to leave comments, you need to log in
Can a value from the Database throw null because it does not have time to load the value?
We need to get the value in onCreate from the database, but for some reason it throws null all keys match the
database:
The code where all this happens:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
nick1 = findViewById(R.id.Nick);
email1 = findViewById(R.id.Email);
avatar1 = findViewById(R.id.avatar);
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("users").child(getUid());
Log.i("deecode",getUid());
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String nick = dataSnapshot.child("username").getValue(String.class);
String email = dataSnapshot.child("account").getValue(String.class);
String Avatar = dataSnapshot.child("GoogleAvatar").getValue(String.class);
nick1.setText(nick);
email1.setText(email);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference`
Answer the question
In order to leave comments, you need to log in
Since they are in a separate layout, you need to initialize like this -
TextView nick1 = navigationView.getHeaderView(0).findViewById(R.id.NickMenuActivity);
TextView email1 = navigationView.getHeaderView(0).findViewById(R.id.EmailMenuActivity);
It also writes to you that the textview is null. Check that the activity_test markup contains a textview with the IDs you are looking for.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question