Reputation: 267
I am trying to use geocoding to get the latitudes/longitudes of a given place by passing a street address. This is my code :
String addr = "Shop No. 2, Payal Cinema, Old Delhi Road, Sec-14, Gurgaon";
Geocoder geocoder= new Geocoder(this, Locale.getDefault());
List<Address> addressList = null;
try {
addressList = geocoder.getFromLocationName(addr, 10);
} catch (IOException e) {}
Address address = addressList.get(0);
if(address.hasLatitude() && address.hasLongitude()){
lat = address.getLatitude();
lon = address.getLongitude();
}
The error is in the call to gecoder.getFromLocationName(addr,10) ( I checked it through System.out.println;things don't get printed to the logcat after that line)
Here is the stack trace from LogCat:
W/dalvikvm(630): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
E/AndroidRuntime(630): FATAL EXCEPTION: main
E/AndroidRuntime(630): java.lang.RuntimeException: Unable to start activity ComponentInfo{apt.kangkan/apt.kangkan.AddrMarkrsonMapActivity}: java.lang.NullPointerException
E/AndroidRuntime(630): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
E/AndroidRuntime(630): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
E/AndroidRuntime(630): at android.app.ActivityThread.access$600(ActivityThread.java:123)
E/AndroidRuntime(630): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
E/AndroidRuntime(630): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(630): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(630): at android.app.ActivityThread.main(ActivityThread.java:4424)
E/AndroidRuntime(630): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(630): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(630): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime(630): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime(630): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(630): Caused by: java.lang.NullPointerException
E/AndroidRuntime(630): at apt.kangkan.AddrMarkrsonMapActivity.onCreate(AddrMarkrsonMapActivity.java:76)
E/AndroidRuntime(630): at android.app.Activity.performCreate(Activity.java:4465)
E/AndroidRuntime(630): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
E/AndroidRuntime(630): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
E/AndroidRuntime(630): ... 11 more
I/dalvikvm(630): threadid=3: reacting to signal 3
I/dalvikvm(630): Wrote stack traces to '/data/anr/traces.txt'
you can see there is a java.lang.NullPointerException. i don't know why its arising. Please Help.
Upvotes: 0
Views: 1157
Reputation: 480
Actually latitude and longitude not found for your address please check "addressList" is null or not if it is not null then continue like below..
if (addressList == null)
{
Log.d(TAG,"Latitude and longitude not found");
}
else {
Address location = addressList.get(0);
latitude = location.getLatitude();
longitude = location.getLongitude();
}
Upvotes: 2