I need to put a Volley key into a TextView inside the NavigationHeader but it keeps giving me this error

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.example.a6thacanect.MainActivity.onCreate(MainActivity.java:106)

This is the whole code I used for the NavigationHeader.

NavigationView navigationView = (NavigationView) findViewById (R.id.nav_view);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle (
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener (toggle);
        toggle.syncState ();
        navigationView.setNavigationItemSelectedListener (this);
        View header = navigationView.getHeaderView (0);
        final SharedPreferences shared = (getSharedPreferences ("Mypref", Context.MODE_PRIVATE));
        String username = shared.getString("username", "");
        String nickname = shared.getString("nickname", "");
        final TextView username1 = findViewById (R.id.username);
        final TextView nickname1 = findViewById (R.id.nickname);
        username1.setText(username);
        nickname1.setText(nickname);
        TextView headUsername = (TextView) findViewById (R.id.usernameHeader);
        TextView headNickname = (TextView) findViewById (R.id.nicknameHeader);
        headUsername.setText ((CharSequence) username1);
        headNickname.setText ((CharSequence) nickname1);

the (MainActivity.java:106) is username1.setText(username);

For now, the initial displays of the NavigationHeader is as follows:

Android Studio [email protected]

but I want it to be:

"(R.id.nickname);" "(R.id.username);"

example:

JohnSmith65 @johnsmith65

I hope you can help me. Thank you.

0 Answers