I tried to get an address from a pointed location on google maps and get this error :
Blockquote FATAL EXCEPTION: main Process: elder.msu.testlocationapp, PID: 23559 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{elder.msu.testlocationapp/elder.msu.testlocationapp.MapsActivity}: java.lang.NullPointerException: Attempt to read from field 'double com.google.android.gms.maps.model.LatLng.latitude' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2366) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2515) at android.app.ActivityThread.access$1000(ActivityThread.java:154) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1379) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:157) at android.app.ActivityThread.main(ActivityThread.java:5571) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635) Caused by: java.lang.NullPointerException: Attempt to read from field 'double com.google.android.gms.maps.model.LatLng.latitude' on a null object reference at elder.msu.testlocationapp.MapsActivity.(MapsActivity.java:32) at java.lang.Class.newInstance(Native Method) at android.app.Instrumentation.newActivity(Instrumentation.java:1068) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2356) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2515) at android.app.ActivityThread.access$1000(ActivityThread.java:154) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1379) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:157) at android.app.ActivityThread.main(ActivityThread.java:5571) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)
I every necessary thing in my manifest. Here is my code for the map activity
public void onMapReady(GoogleMap googleMap) {
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
markerOptions.position(latLng);
markerOptions.title(latLng.latitude + " : " + latLng.longitude);
mMap.clear();
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.addMarker(markerOptions);
}
});
private void getAddress(){
Geocoder geocoder;
final List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
try{
addresses = geocoder.getFromLocation(latitude,longitude,1);
if (addresses!=null && addresses.size() > 0){
address = addresses.get(0).getAddressLine(0);
city = addresses.get(0).getLocality();
}
}catch (IOException e){
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Cannot get data", Toast.LENGTH_SHORT).show();
}
btnOK.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getAddress();
confirmData(address, city);
}
});
}
private void confirmData(String address, String city) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
Bundle bundle = new Bundle();
intent.putExtra("data", bundle);
intent.putExtra("address", address);
intent.putExtra("city", city);
startActivity(intent);
finish();
}