title: Null Pointer Exception in GeoPoint
body: <p>What's wrong with the below code. It is throwing Null Pointer Exception on this line (<code>location.setLatitude(latitude);</code>). I am trying to make a circle as soon as the application is started so that is the reason I am passing location object from my <code>onCreate</code> method.</p>
  
  <pre><code>private Location location;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);    
  
      locationListener = new GPSLocationListener();
  
      locationManager.requestLocationUpdates(
              LocationManager.GPS_PROVIDER, 
              0, 
              0, 
              locationListener);
      mapView = (MapView) findViewById(R.id.mapView);
      listView = (ListView) findViewById(R.id.mylist);
      mapView.setStreetView(true);
      mapView.setBuiltInZoomControls(true);
  
      mapController = mapView.getController();
      mapController.setZoom(15);
  
      GeoPoint initialPoint = new GeoPoint( (int) (36.778261* 1E6), (int) (-119.417932 * 1E6));
      double latitude = initialPoint .getLatitudeE6() / 1E6;
      double longitude = initialPoint .getLongitudeE6() / 1E6;
  
      location.setLatitude(latitude);
      location.setLongitude(longitude);
      locationListener.onLocationChanged(location);
  }
  </code></pre>
  
  <p>Below is the class which take care of LocationUpdate.</p>
  
  <pre><code>private class GPSLocationListener implements LocationListener {
  
      MapOverlay mapOverlay;
  
      public GPSLocationListener(MapView mapView) {
  
      }
  
      @Override
      public void onLocationChanged(Location location) {
          if (location != null) {
              GeoPoint point = new GeoPoint(
                      (int) (location.getLatitude() * 1E6), 
                      (int) (location.getLongitude() * 1E6));
  
              mapController.animateTo(point);
              mapController.setZoom(15);
  
              if (mapOverlay == null) {
                  mapOverlay = new MapOverlay(this,android.R.drawable.star_on);
                  List&lt;Overlay&gt; listOfOverlays = mapView.getOverlays();
                  listOfOverlays.add(mapOverlay);
              }
              mapOverlay.setPointToDraw(point);
              mapView.invalidate();
          }
      }
  
      @Override
      public void onProviderDisabled(String provider) {
      }
  
      @Override
      public void onProviderEnabled(String provider) {
      }
  
      @Override
      public void onStatusChanged(String provider, int status, Bundle extras) {
      }
  }
  </code></pre>
  
  <p>Any suggestions will be of great help.</p>
  
  <p><strong>UPDATE:-</strong></p>
  
  <p>After making changes I am getting </p>
  
  <pre><code>java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
  </code></pre>
  
  <p>Below is my code where I made changes.</p>
  
  <pre><code>GeoPoint initialPoint = new GeoPoint( (int) (36.778261), (int) (-119.417932));
  double latitude = initialPoint .getLatitudeE6() / 1E6;
  double longitude = initialPoint .getLongitudeE6() / 1E6;
  
  this.location = new Location("TechGeeky Randomly Initialized");
  location.setLatitude(latitude);
  location.setLongitude(longitude);
  locationListener.onLocationChanged(location);
  </code></pre>
  
  <p>Is there anything wrong I am doing here?</p>