Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
56,393 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.