0

I am trying to Set Location but getting NullPointerException , i have Provided android.permission.ACCESS_COARSE_LOCATION , android.permission.ACCESS_FINE_LOCATION
In manifest file my Source code is as Follow , i am getting Null pointer on List<Address> addresses = new Geocoder(LocationDemo.this,Locale.getDefault()).getFromLocation(currentLocation.getLatitude(), currentLocation.getLongitude(), 1);

My Coding is As Follow

import java.io.IOException;
import java.util.*;
import android.widget.*;
import android.app.Activity;
import android.os.Bundle;
import android.location.*;
import android.content.*;

public class LocationDemo extends Activity {

 TextView addressText;
 Location currentLocation;
 double currentLatitude;
 double currentLongitude;
 String store;
 @Override

public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     addressText = (TextView)findViewById(R.id.addressText);
     addressText.setText("ready");
     LocationManager locationManager = 
    (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
    LocationListener locationListener = new LocationListener() {

    public void onLocationChanged(Location location) {
        updateLocation(location);
    }
    public void onStatusChanged(
            String provider, int status, Bundle extras) {}
    public void onProviderEnabled(String provider) {}
    public void onProviderDisabled(String provider) {}
};
locationManager.requestLocationUpdates(
        LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
currentLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

try{
    List<Address> addresses = new Geocoder(LocationDemo.this,Locale.getDefault()).getFromLocation(currentLocation.getLatitude(), currentLocation.getLongitude(), 1);        
    StringBuilder result = new StringBuilder();
    if (addresses.size() > 0) {      
            Address address =  addresses.get(0);
            int maxIndex = address.getMaxAddressLineIndex();
            for (int x = 0; x <= maxIndex; x++ ){
                result.append(address.getAddressLine(x));
                //result.append(",");
            }                    
        }
         addressText.setText(result.toString()); 
        Intent send_add = new Intent();
        send_add.putExtra("address",result.toString());
         store = addressText.getText().toString(); 
}
catch(IOException ex)
{
addressText.setText(ex.getMessage().toString());
} 
} 
void updateLocation(Location location){
   currentLocation = location;
   currentLatitude = currentLocation.getLatitude();
  currentLongitude = currentLocation.getLongitude();
 } 
}

i am importing this code in application that extends Service that is trying to obtain Address on startup of service

Ronak Mehta
  • 5,971
  • 5
  • 42
  • 69

3 Answers3

0
LocationListener[] mLocationListeners = new LocationListener[] {
        new LocationListener(LocationManager.GPS_PROVIDER),
        new LocationListener(LocationManager.NETWORK_PROVIDER) };

try this code in your Location Listener.

Vikash Kumar
  • 621
  • 2
  • 6
  • 21
0

Might be you are getting current location as null. as it takes some time to calculate the location .if this is the case , try this

   import java.io.IOException;
    import java.util.*;
    import android.widget.*;
    import android.app.Activity;
    import android.os.Bundle;
    import android.location.*;
    import android.content.*;

    public class LocationDemo extends Activity {

     TextView addressText;
     Location currentLocation;
     double currentLatitude;
     double currentLongitude;
     String store;
     @Override

    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         addressText = (TextView)findViewById(R.id.addressText);
         addressText.setText("ready");
         LocationManager locationManager = 
        (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
        LocationListener locationListener = new LocationListener() {

        public void onLocationChanged(Location location) {
           setAddress(location)
        } 
        public void onStatusChanged(String provider, int status, Bundle extras) {}
        public void onProviderEnabled(String provider) {}
        public void onProviderDisabled(String provider) {}
    };
        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
     }






public void setAddress(Location location){
currentLocation = location;
       currentLatitude = currentLocation.getLatitude();
      currentLongitude = currentLocation.getLongitude();
try{
        List<Address> addresses = new Geocoder(LocationDemo.this,Locale.getDefault()).getFromLocation(currentLocation.getLatitude(), currentLocation.getLongitude(), 1);        
        StringBuilder result = new StringBuilder();
        if (addresses.size() > 0) {      
                Address address =  addresses.get(0);
                int maxIndex = address.getMaxAddressLineIndex();
                for (int x = 0; x <= maxIndex; x++ ){
                    result.append(address.getAddressLine(x));
                    //result.append(",");
                }                    
            }
             addressText.setText(result.toString()); 
            Intent send_add = new Intent();
            send_add.putExtra("address",result.toString());
             store = addressText.getText().toString(); 
    }
    catch(IOException ex)
    {
    addressText.setText(ex.getMessage().toString());
    } 
}
}
Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
  • not working i am importing this code in application that extends Service that is trying to obtain Address on startup of service – Ronak Mehta Feb 27 '12 at 09:29
0

Of course you're getting a null pointer because currentLocation is null till onLocationChanged is called. Thus you have to put your code there:

LocationListener locationListener = new LocationListener() {

    public void onLocationChanged(Location location) {

      // put your code here once you have the location

    }
    public void onStatusChanged(
            String provider, int status, Bundle extras) {}
    public void onProviderEnabled(String provider) {}
    public void onProviderDisabled(String provider) {}
};
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
Noureddine AMRI
  • 2,942
  • 1
  • 21
  • 28