Question

I am making a map application for android which allows the user to place their own points of interest. I wanted to set custom pushpin icons for each type of point placed. When I try to get my custom ItemizedOverlay at run time, I get a NullPointerException.

Add a point to the map:

public void addPointToMap(PointOfInterest pointOfInterest) {

    OurItemizedOverlay itemizedOverlay = getOverlayForPointOfInterest(pointOfInterest); <--- NullPointer here
    //Add item to collection in OurItemized Overlay
    itemizedOverlay.addOverlay(pointOfInterest);
    mapOverlays.clear();
    mapOverlays.add(myLocationOverlay);
    mapOverlays.add(itemizedOverlay);
    mapView.invalidate();
}

Get Overlay:

private OurItemizedOverlay getOverlayForPointOfInterest(PointOfInterest pointOfInterest) {

       String type = pointOfInterest.getType().toLowerCase();

       if(type.equals("monument")) {
           return monumentOverlay;
       }
       else if(type.equals("building")) {
           return buildingOverlay;
       }
       else if(type.equals("atm")) {
           return atmOverlay;
       }
       else if(type.equals("attraction")) {
           return attractionOverlay;
       }
       else if(type.equals("pub")) {
           return pubOverlay;
       }
       else if(type.equals("restaurant")) {
           return restaurantOverlay;
       }
       else if(type.equals("shop")) {
           return shopOverlay;
       }
       else if(type.equals("bridge")) {
           return bridgeOverlay;
       }
       else if(type.equals("station")) {
           return stationOverlay;
       }
       else if(type.equals("cafe")) {
           return cafeOverlay;
       }
       else if(type.equals("hotel")) {
           return hotelOverlay;
       }
       else {
           return defaultOverlay; 
       }
}

Set up Drawables and Overlays:

/**
 * Set the overlay objects up
 */
private void setItemizedOverlays() {

    //SET OVERLAY Items
    monumentOverlay = new OurItemizedOverlay(monumentIcon, this);
    buildingOverlay = new OurItemizedOverlay(buildingIcon, this);
    shopOverlay = new OurItemizedOverlay(shopIcon, this);
    attractionOverlay = new OurItemizedOverlay(attractionIcon, this);
    bridgeOverlay = new OurItemizedOverlay(bridgeIcon, this);
    stationOverlay = new OurItemizedOverlay(stationIcon, this);
    pubOverlay = new OurItemizedOverlay(pubIcon, this);
    restaurantOverlay = new OurItemizedOverlay(restaurantIcon, this);
    cafeOverlay = new OurItemizedOverlay(cafeIcon, this);
    atmOverlay = new OurItemizedOverlay(atmIcon, this);
    hotelOverlay = new OurItemizedOverlay(hotelIcon, this);


}

/**
 * Set the icons for each Drawable object
 */
private void setDrawableIcons() {

    defaultIcon = this.getResources().getDrawable(R.drawable.downarrow);
    monumentIcon = this.getResources().getDrawable(R.drawable.monument);
    shopIcon = this.getResources().getDrawable(R.drawable.shop);
    attractionIcon = this.getResources().getDrawable(R.drawable.attraction);
    buildingIcon = this.getResources().getDrawable(R.drawable.building);
    bridgeIcon = this.getResources().getDrawable(R.drawable.bridge);
    stationIcon = this.getResources().getDrawable(R.drawable.station);
    pubIcon = this.getResources().getDrawable(R.drawable.pub);
    restaurantIcon = this.getResources().getDrawable(R.drawable.restaurant);
    cafeIcon = this.getResources().getDrawable(R.drawable.cafe);
    atmIcon = this.getResources().getDrawable(R.drawable.atm);
    hotelIcon = this.getResources().getDrawable(R.drawable.hotel);


}

StackTrace:

01-29 18:17:11.875: E/AndroidRuntime(12380): FATAL EXCEPTION: main
01-29 18:17:11.875: E/AndroidRuntime(12380): java.lang.NullPointerException
01-29 18:17:11.875: E/AndroidRuntime(12380):    at com.example.mapproject.MainActivity.addPointToMap(MainActivity.java:140)
01-29 18:17:11.875: E/AndroidRuntime(12380):    at com.example.mapproject.MainActivity.onPointsOfInterestDownloaded(MainActivity.java:227)
01-29 18:17:11.875: E/AndroidRuntime(12380):    at com.example.mapproject.DownloadPointsOfInterest.onPostExecute(DownloadPointsOfInterest.java:67)
01-29 18:17:11.875: E/AndroidRuntime(12380):    at com.example.mapproject.DownloadPointsOfInterest.onPostExecute(DownloadPointsOfInterest.java:1)
01-29 18:17:11.875: E/AndroidRuntime(12380):    at android.os.AsyncTask.finish(AsyncTask.java:602)
01-29 18:17:11.875: E/AndroidRuntime(12380):    at android.os.AsyncTask.access$600(AsyncTask.java:156)
01-29 18:17:11.875: E/AndroidRuntime(12380):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
01-29 18:17:11.875: E/AndroidRuntime(12380):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 18:17:11.875: E/AndroidRuntime(12380):    at android.os.Looper.loop(Looper.java:137)
01-29 18:17:11.875: E/AndroidRuntime(12380):    at android.app.ActivityThread.main(ActivityThread.java:4441)
01-29 18:17:11.875: E/AndroidRuntime(12380):    at java.lang.reflect.Method.invokeNative(Native Method)
01-29 18:17:11.875: E/AndroidRuntime(12380):    at java.lang.reflect.Method.invoke(Method.java:511)
01-29 18:17:11.875: E/AndroidRuntime(12380):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
01-29 18:17:11.875: E/AndroidRuntime(12380):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
01-29 18:17:11.875: E/AndroidRuntime(12380):    at dalvik.system.NativeStart.main(Native Method)

Any pointers would be appreciated !

Was it helpful?

Solution

Most likely error- you went into your default case, I don't see an initialization for defaultOverlay in setItemizedOverlays. But this is a good case for a debugger, figure out which branch it went into in getOverlayForPointOfInterest and see why its getting null.

OTHER TIPS

OurItemizedOverlay itemizedOverlay = getOverlayForPointOfInterest(pointOfInterest); <--- NullPointer here
    //Add item to collection in OurItemized Overlay
    itemizedOverlay.addOverlay(pointOfInterest);// here you overlay draw its k..
    mapOverlays.clear();  //now you clear mapoverlay so all overlay clear 
    mapOverlays.add(myLocationOverlay);//again you add maplocation overaly 
    mapOverlays.add(itemizedOverlay);
    mapView.invalidate();

Replace code with this

 mapOverlays.clear(); 
OurItemizedOverlay itemizedOverlay = getOverlayForPointOfInterest(pointOfInterest); <-- 
        //Add item to collection in OurItemized Overlay
        itemizedOverlay.addOverlay(pointOfInterest);
        // mapOverlays.add(myLocationOverlay); 
        mapOverlays.add(itemizedOverlay);
        mapView.invalidate(); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top