title: Attempt to invoke virtual method 'void android.media.AudioManager.setVibrateSetting(int, int)' on a null object reference
body: <p>I am trying to use toggle button.<br><strong>Below is the code:</strong></p>
  
  <pre><code>public class Activity2 extends AppCompatActivity {
      public Activity2() {
          super();
      }
      AudioManager audio; 
      @Nullable
  
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          return inflater.inflate(R.layout.activity2, container, false);
      }
  
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity2);
      }
  
      public void onToggleClicked(View view) {
        //audio  =new AudioManager();
  
          // Is the toggle on?
          boolean on = ((ToggleButton) view).isChecked();
  
          if (on) {
              // Enable vibrate
              audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,
                      AudioManager.VIBRATE_SETTING_ON);
              audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION,
                      AudioManager.VIBRATE_SETTING_ON);
          } else {
              // Disable vibrate
              audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,
                      AudioManager.VIBRATE_SETTING_OFF);
              audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION,
                      AudioManager.VIBRATE_SETTING_OFF);
          }
      }
  
  } 
  </code></pre>
  
  <p><strong>And here is the xml:</strong></p>
  
  <pre><code>&lt;ToggleButton
          android:id="@+id/togglebutton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textOn="Vibrate on"
          android:textOff="Vibrate off"
          android:onClick="onToggleClicked"/&gt;
  </code></pre>
  
  <p>I am trying to use toggle button and it is giving me error:</p>
  
  <blockquote>
    <p>Attempt to invoke virtual method 'void
    android.media.AudioManager.setVibrateSetting(int, int)' on a null
    object reference.</p>
  </blockquote>
  
  <p>Please Help...Thanks!</p>