Force Closed with Null Pointer Exception in Android
<p>I have this below code which I am trying to test out on the Emulator firstly just to make sure, it is working fine, then I can start testing it on real device.</p>

<p>Below code creates a <code>Google Map</code> on the top half of the Android screen and in the Bottom Half the <code>TextView</code>. As far as I know whenever you start your application having Google Map, you need to pass the latitude and longitude co-ordinates from the DDMS perspective. </p>

<p>But in my case, I am not passing any location co-ordinates and this below program is throwing <code>NULL POINTER EXCEPTION</code> at this line-</p>

<pre><code>mScreenPoints = mapView.getProjection().toPixels(pointToDraw, mScreenPoints);
</code></pre>

<p>I am not sure why is it happening, As far as I know, <code>Google Map</code> should get loaded firstly, and then it should wait to pass the location coordinates from the DDMS perspective, but as soon as I launch my application, I get <code>force closed</code> with the <code>NPE</code>.</p>

<p>Any thoughts why is it happening?</p>

<p>Below is the full code-</p>

<pre><code>private MapView mapView;
private ListView listView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mapView = (MapView) findViewById(R.id.mapView);
    listView = (ListView) findViewById(R.id.mylist);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);    

    locationListener = new GPSLocationListener(mapView);

    locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 
            35000, 
            0, 
            locationListener);


    mapView.setStreetView(true);
    mapView.setBuiltInZoomControls(true);
    mapController = mapView.getController();
    mapController.setZoom(15);
}
</code></pre>

<p>Location Update class-</p>

<pre><code>    private class GPSLocationListener implements LocationListener {

    MapOverlay mapOverlay;

    public GPSLocationListener(MapView mapView) {
        mapOverlay = new MapOverlay(this,android.R.drawable.star_on);
        List&lt;Overlay&gt; listOfOverlays = mapView.getOverlays();
        listOfOverlays.add(mapOverlay);
    }

    @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);

            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>Below is the class which draws a Circle on the Map at the Current Location and <strong>NPE happening in this class only</strong>-</p>

<pre><code>    class MapOverlay extends Overlay {
    private GeoPoint pointToDraw;
    int[] imageNames=new int[6];
    private Point mScreenPoints;
    private Bitmap mBitmap;
    private Paint mCirclePaint;


    public MapOverlay(GPSLocationListener gpsLocationListener, int currentUser) {
        imageNames[0]=currentUser;
        mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mCirclePaint.setColor(0x30000000);
        mCirclePaint.setStyle(Style.FILL_AND_STROKE);
        mBitmap = BitmapFactory.decodeResource(getResources(),imageNames[0]);
        mScreenPoints = new Point();
    }

    public void setPointToDraw(GeoPoint point) {
        pointToDraw = point;
    }

    public GeoPoint getPointToDraw() {
        return pointToDraw;
    }

    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
        super.draw(canvas, mapView, shadow);

                   // NPE happening here
        mScreenPoints = mapView.getProjection().toPixels(pointToDraw, mScreenPoints);

        int totalCircle=4;
        int radius=40;
        int centerimagesize=35;

        for (int i = 1; i &lt;= totalCircle; i ++) { 
            canvas.drawCircle(mScreenPoints.x,mScreenPoints.y, i*radius, mCirclePaint); 
        } 

        canvas.drawBitmap(mBitmap, (mScreenPoints.x-(centerimagesize/2)),(mScreenPoints.y-(centerimagesize/2)), null);
        super.draw(canvas,mapView,shadow);

        return true;
    }
}
</code></pre>

<p><strong>Update:-</strong></p>

<p>I found the problem, I guess when I add the overlay to the list, it will always start drawing immediately. It doesn't matter whether or not I get a location. <strong>How can I have a safe and efficient way to add the  to not add the overlay until there is a location to set.?</strong></p>
