B
B
bodusiacool2019-05-27 17:56:17
Java
bodusiacool, 2019-05-27 17:56:17

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:
B9VyG.png
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);

I get this exception
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference`

I'm trying to change these 2 TextViews - GreenCools,[email protected]
This is a separate layout nav_header_test.xml
Could this be causing this error?
z49BH.png
Here is another Debug
pa6hE.png
This is how the xml looks like which I do setContentView
vlPCr.png
activity_test
o0qfxeF.pngITtnBeN.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
bodusiacool, 2019-05-27
@bodusiacool

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);

D
Denis Zagaevsky, 2019-05-27
@zagayevskiy

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 question

Ask a Question

731 491 924 answers to any question