title: Adding a custom listener inside a broadcast receiver gives NPE in Android
body: <p>I have been stuck with a problem here.I am trying to listen to connectivity changes by implementing a broadcast receiver.Also I have initialised a custom listener within the broadcast receiver so as to communicate to the activity about the connectivity changes to show a Crouton Toast.The code for the various classes as below :</p>
  
  <p>The broadcast receiver :</p>
  
  <pre><code>public class NetworkStateReceiver extends BroadcastReceiver {
  
      private NetworkStateListener networkStateListener;
  
      public NetworkStateReceiver() {
      }
  
      public NetworkStateReceiver(NetworkStateListener networkStateListener) {
          this.networkStateListener = networkStateListener;
      }
  
      @Override
      public void onReceive(Context context, Intent intent) {
          Log.d("test", "Network connectivity change ::: " + this.networkStateListener);
          if (intent.getExtras() != null) {
  
              NetworkInfo ni = (NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
              if (ni != null &amp;&amp; ni.getState() == NetworkInfo.State.CONNECTED) {
                  Log.i("test", "Network " + ni.getTypeName() + " connected");
                  networkStateListener.onConnected();
              }
          }
          if (intent.getExtras().getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) {
              Log.d("test", "There's no network connectivity");
              networkStateListener.onDisconnected();
          }
      }
  }
  </code></pre>
  
  <p>The custom listener interface :</p>
  
  <pre><code>public interface NetworkStateListener {
  
      public void onConnected();
  
      public void onDisconnected();
  }
  </code></pre>
  
  <p>The activity in which the listener is implemented and receiver is registered for connectivity changes : </p>
  
  <pre><code>    public class HomeActivity extends AppCompatActivity implements NetworkStateListener {
       private NetworkStateReceiver mReceiver;
       private LocalBroadcastManager mBroadcastManager;
      @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_home);
        mBroadcastManager =    LocalBroadcastManager.getInstance(HomeActivity.this);
          mReceiver = new NetworkStateReceiver(HomeActivity.this);
      }
  
  @Override
      protected void onResume() {
          super.onResume();
          IntentFilter intentFilter = new IntentFilter();
          intentFilter.addAction(android.net.ConnectivityManager.CONNECTIVITY_ACTION);
          mBroadcastManager.registerReceiver(mReceiver, intentFilter);
      }
  
      @Override
      protected void onPause() {
          super.onPause();
          mBroadcastManager.unregisterReceiver(mReceiver);
      }
      }
  </code></pre>
  
  <p>And this is the manifest entry :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.test"&gt;
  
  &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
      &lt;uses-permission android:name="android.permission.RECEIVE_SMS" /&gt;
      &lt;uses-permission android:name="android.permission.READ_SMS" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
      &lt;uses-permission android:name="android.permission.READ_PHONE_STATE" /&gt;
      &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
      &lt;uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /&gt;
  
   &lt;application
          android:name=".TestApplication"
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
  
  &lt;receiver android:name=".shared.receivers.NetworkStateReceiver"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.net.conn.CONNECTIVITY_CHANGE" /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>I have no idea why the networkstateListener is not getting initialised.It gives a null pointer denoting that NetWorkStateListener may not be initialised.Can someone please help me out with this ? I have achieved this in the past, but with a custom broadcast and not with these kind of system broadcasts.</p>