Hasib Hasan
Hasib Hasan

Reputation: 93

How to avoid NullPointerException for LOCATION_SERVICE?

In my App, if my LOCATION_SERVICE is off i am getting the NullPointerException. But i want an alertDialog that will prompt the user of activating LOCATION_SERVICE. Can anyone please just tell me how to handle this exception?

 package com.bddevs.ddbllocator;
   import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends ActionBarActivity {
private GoogleMap googleMap;;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpMapIfNeeded();

}

private void setUpMapIfNeeded() {
if (googleMap == null) {
    googleMap = ((SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map)).getMap();
    if (googleMap != null) {
        setUpMap();
    }
}

}

private void setUpMap() {
googleMap.setMyLocationEnabled(true);
LocationManager locationmanager = (LocationManager) getSystemService(LOCATION_SERVICE);
String provider = LocationManager.NETWORK_PROVIDER;

Location myLocation = locationmanager.getLastKnownLocation(provider);
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
double latitude = myLocation.getLatitude();
double longitude = myLocation.getLongitude();
LatLng latlng = new LatLng(latitude, longitude);
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
    return true;
}
return super.onOptionsItemSelected(item);

}

And here is the LOGCAT TRACE:

03-29 23:52:34.682: E/AndroidRuntime(11716): FATAL EXCEPTION: main 03-29 23:52:34.682: E/AndroidRuntime(11716): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bddevs.ddbllocator/com.bddevs.ddbllocator.DBBL_LOCATOR}: java.lang.NullPointerException 03-29 23:52:34.682: E/AndroidRuntime(11716): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2063) 03-29 23:52:34.682: E/AndroidRuntime(11716): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2088) 03-29 23:52:34.682: E/AndroidRuntime(11716): at android.app.ActivityThread.access$600(ActivityThread.java:134) 03-29 23:52:34.682: E/AndroidRuntime(11716): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199) 03-29 23:52:34.682: E/AndroidRuntime(11716): at android.os.Handler.dispatchMessage(Handler.java:99) 03-29 23:52:34.682: E/AndroidRuntime(11716): at android.os.Looper.loop(Looper.java:137) 03-29 23:52:34.682: E/AndroidRuntime(11716): at android.app.ActivityThread.main(ActivityThread.java:4744) 03-29 23:52:34.682: E/AndroidRuntime(11716): at java.lang.reflect.Method.invokeNative(Native Method) 03-29 23:52:34.682: E/AndroidRuntime(11716): at java.lang.reflect.Method.invoke(Method.java:511) 03-29 23:52:34.682: E/AndroidRuntime(11716): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 03-29 23:52:34.682: E/AndroidRuntime(11716): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 03-29 23:52:34.682: E/AndroidRuntime(11716): at dalvik.system.NativeStart.main(Native Method) 03-29 23:52:34.682: E/AndroidRuntime(11716): Caused by: java.lang.NullPointerException 03-29 23:52:34.682: E/AndroidRuntime(11716): at com.bddevs.ddbllocator.DBBL_LOCATOR.setUpMap(DBBL_LOCATOR.java:73) 03-29 23:52:34.682: E/AndroidRuntime(11716): at com.bddevs.ddbllocator.DBBL_LOCATOR.setUpMapIfNeeded(DBBL_LOCATOR.java:38) 03-29 23:52:34.682: E/AndroidRuntime(11716): at com.bddevs.ddbllocator.DBBL_LOCATOR.onCreate(DBBL_LOCATOR.java:29) 03-29 23:52:34.682: E/AndroidRuntime(11716): at android.app.Activity.performCreate(Activity.java:5008) 03-29 23:52:34.682: E/AndroidRuntime(11716): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 03-29 23:52:34.682: E/AndroidRuntime(11716): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2027) 03-29 23:52:34.682: E/AndroidRuntime(11716): ... 11 more

Upvotes: 0

Views: 99

Answers (1)

Libin
Libin

Reputation: 17095

If location service is disabled . getLastKnownLocation() will return null.

Add a null check

     Location myLocation = locationmanager.getLastKnownLocation(provider);
    If(myLocation == null ){
         // show the alert dialog
    }
   else {
         // do your stuff with latitude and longitude
  }

Upvotes: 1

Related Questions