Question: I already created an android app which uses a web service to send and retrieve JSON data.

When I make a request while the device is online it works fine but when the device goes offline the app is stuck and prints Null Pointer Exception error.

Is there a way to listen to the internet connection?

Answer: 
* You can check if it is connected to internet through this.

```
private boolean isNetworkAvailable() { 
        ConnectivityManager manager = (ConnectivityManager)
                getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        boolean isAvailable = false;

        if(networkInfo != null && networkInfo.isConnected()) {
            isAvailable = true;

        } 

        return isAvailable;
 }

```
In your manifest add permission 

```
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

```

Java 

```
public boolean networkstatus() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
 }

if(networkstatus())
{
// do your process
}
else
{
//alert message for no internet connection
}

```