-3

When i use the below code my app will run:

private ConnectivityManager checknetworkconnectivity;
private NetworkInfo checknetworkinfo;

checknetworkconnectivity = (ConnectivityManager) 
getSystemService(Context.CONNECTIVITY_SERVICE);
    checknetworkinfo = checknetworkconnectivity.getActiveNetworkInfo();

    if(checknetworkinfo != null && checknetworkinfo.isConnectedOrConnecting()){
        LoaderManager manager  = getLoaderManager();
        manager.initLoader(EARTHQUAKE_LOADER_ID, null, EarthquakeActivity.this);
    }
    else {
        progressbar.setVisibility(View.GONE);
        blanktextview.setText(R.string.no_internet);
    }

but when i used the below code app crahes:

   private ConnectivityManager checknetworkconnectivity;
    private NetworkInfo checknetworkinfo;

   checknetworkconnectivity = (ConnectivityManager) 
  getSystemService(Context.CONNECTIVITY_SERVICE);
    checknetworkinfo = checknetworkconnectivity.getActiveNetworkInfo();

    if(checknetworkinfo.isConnectedOrConnecting()&& checknetworkinfo != 
   null){
        LoaderManager manager  = getLoaderManager();
        manager.initLoader(EARTHQUAKE_LOADER_ID, null, EarthquakeActivity.this);
    }
    else {
        progressbar.setVisibility(View.GONE);
        blanktextview.setText(R.string.no_internet);
    }

Why this is happening, i think both are same?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Jan 06 '18 at 14:53
  • I'll be honest, I'm not sure why the downvotes. This is a legitimate question with code samples provided. – ReverseEffect Jan 06 '18 at 15:09

2 Answers2

2

I think its simply a NullPointerException. See the code below.

if(checknetworkinfo.isConnectedOrConnecting()&& checknetworkinfo != null)

Now if checknetworkinfo is Null then it will throw Nullpointer Cause you are checking for null in second condition . You should first check for null and then check for other condition . So it should be as .

if(checknetworkinfo != null && checknetworkinfo.isConnectedOrConnecting())
ADM
  • 20,406
  • 11
  • 52
  • 83
0

As written in the documentation, getActiveNetworkInfo() returns NULL when there is no default currently active network.

Consider a simple AND statement like: <condition A> && <condition B> Since this is an AND operator both the conditions need to be true.
In Java, if the first condition comes out to be false, the second condition is not even executed (or checked) as it won't make any difference whether it is true or false. (AND operator would still return false)

In if(checknetworkinfo != null && checknetworkinfo.isConnectedOrConnecting()) the variable checknetworkinfo can never be NULL during the execution of the second condition, as the first condition must always be true before the second can be checked.
Howerver, in if(checknetworkinfo.isConnectedOrConnecting()&& checknetworkinfo != null) the variable checknetworkinfo is not checked for NULL. And so when it is NULL, it results in a NullPointerException.

fsljfke
  • 431
  • 6
  • 14