public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { ApiCaller userInfo; User myAccount; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); userInfo = Core.buildInterceptCaller(); Call<User> call = userInfo.showAccount(); call.enqueue(new Callback<User>() { @Override public void onResponse(Call<User> call, Response<User> response) { if (response.isSuccessful()) myAccount = response.body(); } @Override public void onFailure(Call<User> call, Throwable t) { } }); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.addDrawerListener(toggle); toggle.syncState(); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); View headerView = navigationView.getHeaderView(0); TextView nickname = (TextView)headerView.findViewById(R.id.txt_nickname); nickname.setText(myAccount.getNickName()); } @Override public void onBackPressed() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. int id = item.getItemId(); if (id == R.id.nav_camera) { // Handle the camera action } else if (id == R.id.nav_gallery) { } else if (id == R.id.nav_slideshow) { } else if (id == R.id.nav_manage) { } else if (id == R.id.nav_share) { } else if (id == R.id.nav_send) { } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; } } 

Here is the code. I want to nick the nickname of the received user into the NawDrawer header, but it climbs like this:

java.lang.RuntimeException: Unable to start activity ComponentInfo {com.eleks.fry.imessmobile / com.eleks.fry.imessmobile.activities.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.blahblah.blah.blahblah.models.User.getNickName () 'on a null object reference

Help solve the problem?

  • Check through logging, what nickname Retrofit - timuruktus returns to you

2 answers 2

The template clearly tells you that the getNickName() method cannot be called because the User object refers to null

    You are trying to use myAccount right after launching the request, at that moment when it has not finished yet. call to call.enqueue() is asynchronous. Accordingly, the onResponse() method onResponse() called only after some time after the launch of the request.

    Transfer this code to the onResponse() method

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); View headerView = navigationView.getHeaderView(0); TextView nickname = (TextView)headerView.findViewById(R.id.txt_nickname); nickname.setText(myAccount.getNickName());