Java.lang.NullPointerException in navigation Drawer

I am new in android development and I am trying to make navigation drawer. As soon as I put these lines in my java file, my application unfortunately stops. Can somebody tell me what am I doing wrong with the code? It does not say anything while building the project, no syntax errors nothing but when I hover over one of these two lines it says this:

"Method invocation in `getSupportActionBar.setHomeButtonEnabled may produce java.lang.NullPointerException"

getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

This is my MainActivity.java file:

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.DrawableRes;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.widget.DrawerLayout;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;



public class MainActivity extends AppCompatActivity  implements AdapterView.OnItemClickListener {

    private ActionBarDrawerToggle actionBarDrawerToggle;
    private DrawerLayout drawerLayout;
    private ListView navList;

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



        drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,
                R.string.close_drawer,R.string.open_drawer);
        drawerLayout.setDrawerListener(actionBarDrawerToggle);


        //NullPointException in these two lines
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        navList = (ListView)findViewById(R.id.list_items);
        ArrayList<String> navArray = new ArrayList<String>();
        navArray.add("Home");
        navArray.add("Call Logs");
        navArray.add("Message Logs");
        navArray.add("Location");
        navArray.add("Browsing History");
        navArray.add("Dictionary");
        navArray.add("About Us");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,navArray);
        navList.setAdapter(adapter);
        navList.setOnItemClickListener(this);

        loadSelection(0);


    }
    private void loadSelection(int i){
        navList.setItemChecked(i, true);
    }

    @Override
    public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onPostCreate(savedInstanceState, persistentState);
        actionBarDrawerToggle.syncState();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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;
        }
        else if(id == android.R.id.home)
        {
            if(drawerLayout.isDrawerOpen(navList))
            {
                drawerLayout.closeDrawer(navList);
            }
            else
            {
                drawerLayout.openDrawer(navList);
            }
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch(position){
            case 0:

                break;
            case 1:

                break;
            case 2:

                break;
            case 3:

                break;
            case 4:

                break;
            case 5:

                break;
            case 6:

                break;
            case 7:

                break;
        }
        drawerLayout.closeDrawer(navList);

    }


    }

Answers


This is because you don't know if "getSupportActionBar" will return an "ActionBar" or "null".

You have two options available:

  • You are certain that you will always have a ActionBar, and so you can ignore this warning.
  • You are not sure or you like to have an aesthetic code without warnings, and so you can do this :

    ActionBar actionBar = this.getSupportActionBar();
    if (actionBar != null) actionBar.setDisplayHomeAsUpEnabled(true);
    

By the way, like you can see here, you don't need "setHomeButtonEnabled".


Need Your Help

Backbone.history.navigate(url,{trigger:true, replace: true})

javascript url backbone.js browser-history

I used this method to navigate to url, trigger an event and not push url to browser history. But Backbone.history.navigate(url,{trigger:true, replace: true}) replace previous url from history.