title: An error occured while checking mobile data state
body: <p>I want to check the mobile data state on/off in android.
  I have written the below code in button clicking code..</p>
  
  <pre><code>@SuppressWarnings("rawtypes")
  public void check_mobile_data(View view) { // this is my button clicking event.
      Context context = null;
  
      boolean mobileDataEnabled = false; // Assume disabled
      @SuppressWarnings("null")
      ConnectivityManager cm = (ConnectivityManager) context
              .getSystemService(Context.CONNECTIVITY_SERVICE);
      try {
  
          Class cmClass = Class.forName(cm.getClass().getName());
  
          @SuppressWarnings("unchecked")
          Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
          method.setAccessible(true); // Make the method callable
          // get the setting for "mobile data"
          mobileDataEnabled = (Boolean) method.invoke(cm);
      } catch (Exception e) {
          e.printStackTrace();
          // Some problem accessible private API
          // TODO do whatever error handling you want here
      }
  
      if (mobileDataEnabled == true) {
          Toast.makeText(getApplicationContext(), "ON", Toast.LENGTH_LONG)
                  .show();
      }
      if (mobileDataEnabled == false) {
          Toast.makeText(getApplicationContext(), "OFF", Toast.LENGTH_LONG)
                  .show();
      }
  }
  </code></pre>
  
  <p>The below error is showed by the logcat</p>
  
  <pre><code>02-19 11:08:29.338: E/AndroidRuntime(14900): FATAL EXCEPTION: main
  02-19 11:08:29.338: E/AndroidRuntime(14900): Process: com.example.checkmobiledata, PID: 14900
  02-19 11:08:29.338: E/AndroidRuntime(14900): java.lang.IllegalStateException: Could not execute method of the activity
  02-19 11:08:29.338: E/AndroidRuntime(14900):    at android.view.View$1.onClick(View.java:4012)
  02-19 11:08:29.338: E/AndroidRuntime(14900):    at android.view.View.performClick(View.java:4761)
  02-19 11:08:29.338: E/AndroidRuntime(14900):    at android.view.View$PerformClick.run(View.java:19767)
  02-19 11:08:29.338: E/AndroidRuntime(14900):    at android.os.Handler.handleCallback(Handler.java:739)
  02-19 11:08:29.338: E/AndroidRuntime(14900):    at android.os.Handler.dispatchMessage(Handler.java:95)
  02-19 11:08:29.338: E/AndroidRuntime(14900):    at android.os.Looper.loop(Looper.java:135)
  02-19 11:08:29.338: E/AndroidRuntime(14900):    at android.app.ActivityThread.main(ActivityThread.java:5312)
  02-19 11:08:29.338: E/AndroidRuntime(14900):    at java.lang.reflect.Method.invoke(Native Method)
  02-19 11:08:29.338: E/AndroidRuntime(14900):    at java.lang.reflect.Method.invoke(Method.java:372)
  02-19 11:08:29.338: E/AndroidRuntime(14900):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
  02-19 11:08:29.338: E/AndroidRuntime(14900):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
  02-19 11:08:29.338: E/AndroidRuntime(14900): Caused by: java.lang.reflect.InvocationTargetException
  02-19 11:08:29.338: E/AndroidRuntime(14900):    at java.lang.reflect.Method.invoke(Native Method)
  02-19 11:08:29.338: E/AndroidRuntime(14900):    at java.lang.reflect.Method.invoke(Method.java:372)
  02-19 11:08:29.338: E/AndroidRuntime(14900):    at android.view.View$1.onClick(View.java:4007)
  02-19 11:08:29.338: E/AndroidRuntime(14900):    ... 10 more
  02-19 11:08:29.338: E/AndroidRuntime(14900): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
  02-19 11:08:29.338: E/AndroidRuntime(14900):    at com.example.checkmobiledata.MainActivity.check_mobile_data(MainActivity.java:28)
  02-19 11:08:29.338: E/AndroidRuntime(14900):    ... 13 more
  </code></pre>
  
  <p>Please give me reply at your best.</p>