title: How to fix Null pointer exception
body: <p>I am writing an android application that consists of a alertdialog and i am getting a null pointer exception.I cant find any exceptions in my code but it was showing null pointer exception please tell me where i did the mistake</p>
  
  <p>Here i used the library  downloaded from <a href="https://github.com/navasmdc/MaterialDesignLibrary" rel="nofollow">here</a> for material design for buttons here.this is my activity</p>
  
  <pre><code>package com.developer.milanandroid;
  
  
  import java.io.IOException;
  import android.app.ActionBar;
  import android.app.Activity;
  import android.app.Dialog;
  import android.content.Intent;
  import android.content.SharedPreferences;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.Window;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import com.gc.materialdesign.views.ButtonFloatSmall;
  import com.gc.materialdesign.widgets.SnackBar;
  import com.milan.paperbuttons.signoutoptionsbuttons;
  
  public class Modes extends Activity {
      public static final String PREFS_NAME = "LoginPreferences";
       Button auto,manual,adminsettings;
       ActionBar action_bar;
       TextView welcome_headder;
       String welcome_headder_string;
       ButtonFloatSmall options;
       signoutoptionsbuttons signout,shutdown,restart;
       Dialog sign_out_dialog;
       @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.modes);
          action_bar = getActionBar();
          action_bar.hide();
          welcome_headder = (TextView)findViewById(R.id.welcome_notation);
          Intent welcome_headder_intent = getIntent();
          welcome_headder_string= welcome_headder_intent.getStringExtra("USERNAME");
          welcome_headder.setText("Welcome, "+welcome_headder_string);
  
          options = (ButtonFloatSmall)findViewById(R.id.options_signout);
          options.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              Toast.makeText(getApplicationContext(), "Testing",Toast.LENGTH_LONG).show();
  
  
              sign_out_dialog = new Dialog(Modes.this);
              sign_out_dialog.setTitle("Signout options");
              sign_out_dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.shutdown);
              sign_out_dialog.setContentView(R.layout.signout_options);
  
              signout = (signoutoptionsbuttons)sign_out_dialog.findViewById(R.id.signout);
              signout.setOnClickListener(new View.OnClickListener() {
  
                  @Override
                  public void onClick(View v) {
                      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                      SharedPreferences.Editor editor = settings.edit();
                      editor.remove("logged");
                      editor.commit();
                      System.exit(0);
  
                  }
              });
  
          restart = (signoutoptionsbuttons)sign_out_dialog.findViewById(R.id.restart);
          restart.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
  
                  new SnackBar(Modes.this, "This will Signout and restart the panel.Are yout sure you want to Restart the panel !","Yes",new OnClickListener() {
  
                      @Override
                      public void onClick(View v) {
                          SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                          SharedPreferences.Editor editor = settings.edit();
                          editor.remove("logged");
                          editor.commit();
  
                          try
                          {
                              Process proc = Runtime.getRuntime()
                                      .exec(new String[]{ "su", "-c", "reboot" });
                          }
                          catch(Exception e)
                          {
                              Toast.makeText(Modes.this, e.getMessage(), Toast.LENGTH_LONG).show();
                          }
  
                      }
                  }).show();
              }
          });
  
  
          shutdown = (signoutoptionsbuttons)sign_out_dialog.findViewById(R.id.signout_shutdown);
          shutdown.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
              new SnackBar(Modes.this, "Are you sure you want to shut down the panel", "yes", new View.OnClickListener() {
  
                  @Override
                  public void onClick(View v) {
                      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                      SharedPreferences.Editor editor = settings.edit();
                      editor.remove("logged");
                      editor.commit();
  
                      try
                      {
                          Process proc = Runtime.getRuntime()
                                  .exec(new String[]{ "su", "-c", "reboot -p" });
                      }catch (IOException e)
                      {
  
                         Toast.makeText(Modes.this, e.getMessage(), Toast.LENGTH_LONG).show();                                   
                      }
  
                  }
              }).show();
          }
      });
  }
  });
  sign_out_dialog.show();
  
          auto = (Button)findViewById(R.id.button_auto);
          auto.setOnClickListener(new View.OnClickListener() {
  
              public void onClick(View v) {
                  Intent auto = new Intent(Modes.this,Auto.class);
                  auto.putExtra("USERNAME",welcome_headder_string);
                  startActivity(auto);
              }
          });
  
          manual = (Button)findViewById(R.id.button_manualmode);
          manual.setOnClickListener(new View.OnClickListener() {
  
              public void onClick(View v) {
  
                  Intent manual = new Intent(Modes.this,Manual.class);
                  startActivity(manual);
  
              }
          });
          adminsettings = (Button)findViewById(R.id.button_Adminsettings);
  
          savedInstanceState = getIntent().getExtras();
          if(savedInstanceState!=null){
              String value_username = savedInstanceState.getString("USERNAME"); //getting username key,value pairs sent from previous intents
  
              String value_password = savedInstanceState.getString("PASSWORD"); //getting password key,value pairs sent from previous intents
  
              if(value_username.equals("medequip") &amp;&amp; value_password.equals("medequip")){   //Comparing username and password sent from previous intent activity to display admin settings button or not...
  
              adminsettings.setVisibility(View.VISIBLE);                          //setting visibility for a button
  
              }
              else
              adminsettings.setVisibility(View.GONE);
          }
  
          adminsettings.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  Intent retriving_database = new Intent (Modes.this,AdminSettings.class);
                  startActivity(retriving_database);
              }
          });
  
  
  
  
  
  }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.modes, 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();
          if (id == R.id.action_settings) {
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  }
  </code></pre>
  
  <p>This is my XMl:</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      xmlns:materialdesign="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context="com.developer.milanandroid.Modes" &gt;
  
      &lt;RelativeLayout
          android:id="@+id/relativelayout"
          android:layout_width="fill_parent"
          android:layout_height="150dp"
          android:background="#FFC107"&gt;
  
          &lt;TextView
              android:id="@+id/textView1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentBottom="true"
              android:layout_centerHorizontal="true"
              android:text="MODES"
              android:textColor="#FFFFFF"
              android:textSize="75dp"
              android:textAppearance="?android:attr/textAppearanceLarge" /&gt;
  
          &lt;TextView
              android:id="@+id/welcome_notation"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentTop="true"
              android:layout_marginLeft="152dp"
              android:layout_toRightOf="@+id/textView1"
              android:gravity="right"
              android:textColor="#FFFFFF" /&gt;
  
      &lt;/RelativeLayout&gt;
  
      &lt;LinearLayout
          android:id="@+id/linearLayout1"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_below="@+id/relativelayout"
          android:layout_marginTop="44dp"
          android:layout_weight="1"
          android:orientation="vertical"
          android:padding="6dip" &gt;
  
          &lt;LinearLayout
              android:id="@+id/linear"
              android:layout_width="fill_parent"
              android:layout_height="150dp"
              android:orientation="horizontal" &gt;
  
              &lt;Button
                  android:id="@+id/button_auto"
                  style="@style/HomeButton"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:drawableTop="@drawable/automode"
                  android:text="@string/automode" /&gt;
  
              &lt;Button
                  android:id="@+id/button_manualmode"
                  style="@style/HomeButton"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:drawableTop="@drawable/manualmode"
                  android:text="@string/manualmode" /&gt;
          &lt;/LinearLayout&gt;
  
          &lt;LinearLayout
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:orientation="horizontal" &gt;
  
              &lt;Button
                  android:id="@+id/buttoncalibrationmode"
                  style="@style/HomeButton"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:drawableTop="@drawable/calibration"
                  android:text="@string/calibration" /&gt;
  
              &lt;Button
                  android:id="@+id/button_manualmode_2"
                  style="@style/HomeButton"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:drawableTop="@drawable/manualmode"
                  android:text="@string/manualmode" /&gt;
          &lt;/LinearLayout&gt;
  
          &lt;LinearLayout
              android:layout_width="fill_parent"
              android:layout_height="150dp"
              android:orientation="horizontal" &gt;
  
              &lt;Button
                  android:id="@+id/button_Review"
                  style="@style/HomeButton"
                  android:layout_width="380dp"
                  android:layout_height="wrap_content"
                  android:drawableTop="@drawable/review"
                  android:text="@string/Review" /&gt;
  
              &lt;Button
                  android:id="@+id/button_Adminsettings"
                  style="@style/HomeButton"
                  android:layout_width="380dp"
                  android:layout_height="wrap_content"
                  android:drawableTop="@drawable/adminsettings"
                  android:text="@string/Adminsettings" /&gt;
          &lt;/LinearLayout&gt;
      &lt;/LinearLayout&gt;
  
        &lt;com.gc.materialdesign.views.ButtonFloatSmall
          android:id="@+id/options_signout"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentRight="true"
          android:layout_alignParentTop="true"
          android:layout_marginRight="72dp"
          android:layout_marginTop="128dp"
          android:background="#FF8F00"
          android:drawable="@drawable/usr" &gt;
      &lt;/com.gc.materialdesign.views.ButtonFloatSmall&gt;
  
     &lt;!--   &lt;com.gc.materialdesign.view.ButtonFloatSmall
          android:id="@+id/options_sign_out"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentRight="true"
          android:layout_alignParentTop="true"
          android:layout_marginRight="75dp"
          android:layout_marginTop="128dp"
          android:background="#FF8F00"&gt;&lt;/com.gc.materialdesign.view.ButtonFloatSmall&gt;--&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>This is my custom dialog layout:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:widget="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="match_parent" &gt;
  
      &lt;com.milan.paperbuttons.signoutoptionsbuttons
          android:id="@+id/signout"
          android:layout_width="350dp"
          android:layout_height="50dp"
          android:layout_alignParentTop="true"
          android:layout_centerHorizontal="true"
          android:layout_marginTop="57dp" 
          widget:paper_text="Signout"
          /&gt;
  
        &lt;com.milan.paperbuttons.signoutoptionsbuttons
            android:id="@+id/restart"
            android:layout_width="350dp"
            android:layout_height="50dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="118dp"
            widget:paper_text="Signout and Restart " /&gt;s
  
        &lt;com.milan.paperbuttons.signoutoptionsbuttons
            android:id="@+id/signout_shutdown"
            android:layout_width="350dp"
            android:layout_height="50dp"
            android:layout_alignLeft="@+id/restart"
            android:layout_below="@+id/restart"
            android:layout_marginTop="22dp"
            widget:paper_text="Signout and Shutdown" /&gt;
  
        &lt;com.gc.materialdesign.views.ButtonFlat
            android:id="@+id/dialog_return"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/signout_shutdown"
            android:layout_marginRight="96dp"
            android:layout_marginTop="23dp"
            android:text="@string/back"
            android:textColor="#ffffff" &gt;
        &lt;/com.gc.materialdesign.views.ButtonFlat&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>This is my logcat:</p>
  
  <pre><code>01-02 10:22:30.070: E/AndroidRuntime(13445): FATAL EXCEPTION: main
  01-02 10:22:30.070: E/AndroidRuntime(13445): Process: com.developer.milanandroid, PID: 13445
  01-02 10:22:30.070: E/AndroidRuntime(13445): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.developer.milanandroid/com.developer.milanandroid.Modes}: java.lang.NullPointerException
  01-02 10:22:30.070: E/AndroidRuntime(13445):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
  01-02 10:22:30.070: E/AndroidRuntime(13445):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
  01-02 10:22:30.070: E/AndroidRuntime(13445):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
  01-02 10:22:30.070: E/AndroidRuntime(13445):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
  01-02 10:22:30.070: E/AndroidRuntime(13445):    at android.os.Handler.dispatchMessage(Handler.java:102)
  01-02 10:22:30.070: E/AndroidRuntime(13445):    at android.os.Looper.loop(Looper.java:136)
  01-02 10:22:30.070: E/AndroidRuntime(13445):    at android.app.ActivityThread.main(ActivityThread.java:5017)
  01-02 10:22:30.070: E/AndroidRuntime(13445):    at java.lang.reflect.Method.invokeNative(Native Method)
  01-02 10:22:30.070: E/AndroidRuntime(13445):    at java.lang.reflect.Method.invoke(Method.java:515)
  01-02 10:22:30.070: E/AndroidRuntime(13445):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
  01-02 10:22:30.070: E/AndroidRuntime(13445):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
  01-02 10:22:30.070: E/AndroidRuntime(13445):    at dalvik.system.NativeStart.main(Native Method)
  01-02 10:22:30.070: E/AndroidRuntime(13445): Caused by: java.lang.NullPointerException
  01-02 10:22:30.070: E/AndroidRuntime(13445):    at com.developer.milanandroid.Modes.onCreate(Modes.java:132)
  01-02 10:22:30.070: E/AndroidRuntime(13445):    at android.app.Activity.performCreate(Activity.java:5231)
  01-02 10:22:30.070: E/AndroidRuntime(13445):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
  01-02 10:22:30.070: E/AndroidRuntime(13445):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
  01-02 10:22:30.070: E/AndroidRuntime(13445):    ... 11 more
  </code></pre>