Note that there are some explanatory texts on larger screens.

plurals
  1. PONullPointerException when using Google maps, android
    primarykey
    data
    text
    <p>I am using Google maps in my android code, I want to get the address of my location using "Geocoder" class.</p> <p>this is the code I am using </p> <pre><code>Geocoder geocoder; List&lt;Address&gt; addresses = null; geocoder = new Geocoder(this, Locale.getDefault()); try { addresses = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } </code></pre> <p>but I am getting a NullPointerException at this line:</p> <pre><code>addresses = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1); </code></pre> <p>here is the full code</p> <pre><code>public class MainActivity extends Activity implements LocationListener { private TextView cityText; private TextView condDescr; private TextView temp; private TextView press; private TextView windSpeed; private TextView windDeg; private TextView hum; private ImageView imgView; Geocoder geocoder; String bestProvider; List&lt;Address&gt; user = null; double lat; double lng; public static String resultStr; private String latituteField; private String longitudeField; private LocationManager locationManager; private String provider; public String cityName; public Location loc; @Override public void onLocationChanged(android.location.Location loc) { // TODO Auto-generated method stub String longitude = "Longitude: " +loc.getLongitude(); // Log.v(TAG, longitude); String latitude = "Latitude: " +loc.getLatitude(); //Log.v(TAG, latitude); //String cityName=null; Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault()); List&lt;Address&gt; addresses; try { addresses = gcd.getFromLocation(loc.getLatitude(), loc .getLongitude(), 1); if (addresses.size() &gt; 0) System.out.println(addresses.get(0).getLocality()); cityName=addresses.get(0).getLocality(); } catch (IOException e) { e.printStackTrace(); } String s = longitude+"\n"+latitude + "\n\nMy Currrent City is: "+cityName; // editLocation.setText(s); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d("hi","hi"); Geocoder geocoder; List&lt;Address&gt; addresses = null; geocoder = new Geocoder(this, Locale.getDefault()); try { addresses = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Log.d("addresses",addresses.toString()); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // Define the criteria how to select the locatioin provider -&gt; use // default Criteria criteria = new Criteria(); provider = locationManager.getBestProvider(criteria, false); android.location.Location location = locationManager.getLastKnownLocation(provider); // Initialize the location fields if (location != null) { System.out.println("Provider " + provider + " has been selected."); onLocationChanged(location); } else { System.out.println("location not available"); } // String city ="lat=%f&amp;lon=%f," ///////////////////////// cityText = (TextView) findViewById(R.id.cityText); condDescr = (TextView) findViewById(R.id.condDescr); temp = (TextView) findViewById(R.id.temp); hum = (TextView) findViewById(R.id.hum); press = (TextView) findViewById(R.id.press); windSpeed = (TextView) findViewById(R.id.windSpeed); windDeg = (TextView) findViewById(R.id.windDeg); imgView = (ImageView) findViewById(R.id.condIcon); JSONWeatherTask task = new JSONWeatherTask(); //task.execute(new String[]{city}); task.execute(new String[]{cityName}); } @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; } private class JSONWeatherTask extends AsyncTask&lt;String, Void, Weather&gt; { @Override protected Weather doInBackground(String... params) { Weather weather = new Weather(); String data = ( (new WeatherHttpClient()).getWeatherData(params[0])); try { weather = JSONWeatherParser.getWeather(data); // Let's retrieve the icon weather.iconData = ( (new WeatherHttpClient()).getImage(weather.currentCondition.getIcon())); } catch (JSONException e) { e.printStackTrace(); } return weather; } @Override protected void onPostExecute(Weather weather) { super.onPostExecute(weather); if (weather.iconData != null &amp;&amp; weather.iconData.length &gt; 0) { Bitmap img = BitmapFactory.decodeByteArray(weather.iconData, 0, weather.iconData.length); imgView.setImageBitmap(img); } cityText.setText(weather.location.getCity() + "," + weather.location.getCountry()); condDescr.setText(weather.currentCondition.getCondition() + "(" + weather.currentCondition.getDescr() + ")"); temp.setText("" + Math.round((weather.temperature.getTemp() - 275.15)) + "°C"); hum.setText("" + weather.currentCondition.getHumidity() + "%"); press.setText("" + weather.currentCondition.getPressure() + " hPa"); windSpeed.setText("" + weather.wind.getSpeed() + " mps"); windDeg.setText("" + weather.wind.getDeg() + "°"); } } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } } </code></pre> <p>and this is the logCat:</p> <pre><code>11-09 04:30:55.723: E/AndroidRuntime(1868): FATAL EXCEPTION: main 11-09 04:30:55.723: E/AndroidRuntime(1868): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.survivingwithandroid.weatherapp/com.survivingwithandroid.weatherapp.MainActivity}: java.lang.NullPointerException 11-09 04:30:55.723: E/AndroidRuntime(1868): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 11-09 04:30:55.723: E/AndroidRuntime(1868): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 11-09 04:30:55.723: E/AndroidRuntime(1868): at android.app.ActivityThread.access$600(ActivityThread.java:141) 11-09 04:30:55.723: E/AndroidRuntime(1868): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 11-09 04:30:55.723: E/AndroidRuntime(1868): at android.os.Handler.dispatchMessage(Handler.java:99) 11-09 04:30:55.723: E/AndroidRuntime(1868): at android.os.Looper.loop(Looper.java:137) 11-09 04:30:55.723: E/AndroidRuntime(1868): at android.app.ActivityThread.main(ActivityThread.java:5103) 11-09 04:30:55.723: E/AndroidRuntime(1868): at java.lang.reflect.Method.invokeNative(Native Method) 11-09 04:30:55.723: E/AndroidRuntime(1868): at java.lang.reflect.Method.invoke(Method.java:525) 11-09 04:30:55.723: E/AndroidRuntime(1868): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 11-09 04:30:55.723: E/AndroidRuntime(1868): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 11-09 04:30:55.723: E/AndroidRuntime(1868): at dalvik.system.NativeStart.main(Native Method) 11-09 04:30:55.723: E/AndroidRuntime(1868): Caused by: java.lang.NullPointerException 11-09 04:30:55.723: E/AndroidRuntime(1868): at com.survivingwithandroid.weatherapp.MainActivity.onCreate(MainActivity.java:127) 11-09 04:30:55.723: E/AndroidRuntime(1868): at android.app.Activity.performCreate(Activity.java:5133) 11-09 04:30:55.723: E/AndroidRuntime(1868): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 11-09 04:30:55.723: E/AndroidRuntime(1868): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload