title: How to fix getActionBar method may produce java.lang.NullPointerException
body: <p>I am using a toolbar as my actionbar in an activity.  I am trying to add the method <code>getActionBar().setDisplayHomeAsUpEnabled(true);</code> to the Activity.java file for Up navigation for older devices.</p>
  
  <p>The method produces the following error message in Android Studio:</p>
  
  <blockquote>
    <p>Method invocation may produce java.lang.NullPointerException</p>
  </blockquote>
  
  <p>The Up navigation on the toolbar works fine on newer devices...now I'm trying to figure out how to make sure it will work for older devices.
  Please advise.</p>
  
  <p>From build.gradle:</p>
  
  <pre><code>dependencies {
     compile "com.android.support:appcompat-v7:22.1.0"
  }
  </code></pre>
  
  <p>From AndroidManifest.xml:</p>
  
  <pre><code>android:theme="@style/Theme.AppCompat.NoActionBar.FullScreen" 
  </code></pre>
  
  <p>From styles.xml</p>
  
  <pre><code>&lt;style name="Theme.AppCompat.NoActionBar.FullScreen" parent="AppTheme"&gt;
  &lt;item name="android:windowNoTitle"&gt;true&lt;/item&gt;
  &lt;item name="windowActionBar"&gt;false&lt;/item&gt;
  &lt;item name="android:windowFullscreen"&gt;true&lt;/item&gt;
  </code></pre>
  
  <p>from Activity.java</p>
  
  <pre><code>public class CardViewActivity extends AppCompatActivity {
  
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.cardviewinput);
  
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  
      if (toolbar != null) {
          // Up navigation to the parent activity for 4.0 and earlier
          getActionBar().setDisplayHomeAsUpEnabled(true);
          toolbar.setNavigationIcon(R.drawable.ic_action_previous_item);
          toolbar.setNavigationOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  onBackPressed();
              }
          });
      }
  
  }
  </code></pre>