-1

I am working on android application in which i am using a logic to find the direction of a person from coordinated. Everything is working fine but i got error at console: : "Dead Code". My code is given below, please explain me this thing.

private void direction() {

        String userLocation = mLatitude + ","
                + mLongitude ;

        if(userLocation!=null){
            String distLocation = Constants.sMY_LOCATION.getLatitude() + ","
                    + Constants.sMY_LOCATION.getLongitude();
            String url = "https://maps.google.com/maps?f=d&saddr=" + userLocation
                    + "&daddr=" + distLocation;
            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(i);
        }else{ // Getting Dead Code Yellow error here
            finish();
            Toast.makeText(getBaseContext(), "Please check your internet and try again!", Toast.LENGTH_SHORT).show();
        }


    }
Usman Khan
  • 3,739
  • 6
  • 41
  • 89

3 Answers3

6

It's because your else can't be reached

String userLocation = mLatitude + ","
            + mLongitude ;

    if(userLocation!=null){
       ...
    }else{
       ...
    }

userLocation will never be null

Plumillon Forge
  • 1,659
  • 1
  • 16
  • 31
2

The code in your else{} will never be reached because your string userLocation is initialized before the if statement, meaning it will never be null.

So the code in you else is effectively dead code.

mlumeau
  • 821
  • 1
  • 8
  • 20
1

You should check mLatitude and mLongitude for being null instead of whole String.

Example:

if (mLatitude != null && mLongitude != null) {
   // String userLocation = ...
}
else {
   // Your else code
}
Artrmz
  • 340
  • 2
  • 14