Android Build a Weather App Working with JSON Exploring the Data

if i use conditions such as if(networkInfo.isConnected() && networkInfo!=null) getting error

Sir since && first checks the left side of the operation if(networkInfo!=null && networkInfo.isConnected()) is running without any error but if i use conditions such as if(networkInfo.isConnected() && networkInfo!=null) if(networkInfo!=null & networkInfo.isConnected()) or if(networkInfo.isConnected()) is facing error at runtimeLogcat says that it is runtime exception so i want to know when to use it!!!!

1 Answer

Seth Kroger
Seth Kroger
Treehouse Moderator 56,252 Points

if(networkInfo.isConnected() && networkInfo!=null) and if(networkInfo.isConnected()) are both really the same. You are calling the isConnected() method before making the null check since the two conditions of the && are evaluated left to right. If networkInfo is null you'll get a exception for calling a method on a null object. So, it you don't get the exception you already know it isn't null.

With the other case, & is a different operator than &&. && is logical-AND and takes two true/false conditions on each side. & is called bitwise-AND and manipulates the binary digits of numbers, as they are represented in the computer memory. You will rarely, if ever, use bitwise operators these days though there are some special cases.