Search code examples
javaandroidgoogle-mapsnullpointerexceptionitemizedoverlay

Extended OverlayItem causes program to crash with NullPointerException


I have a class which extends OverlayItem to include more fields. I have changed all the OverlayItem occurrences in my program with a PointOfInterest class.

When I try to run the same program which worked for OverlayItem, it crashes with a nullpointerException after calling populate() in my ItemizedOverlay class. The Log prints out the correct value. Here is the method it crashes in.

protected void addOverlay(PointOfInterest pointOfInterest) {

    mapOverlays.add(pointOfInterest);
    setLastFocusedIndex(-1);
    Log.d("Add Overlay", pointOfInterest.getDescription());
    populate(); <-- Crashes here
}

And the error

12-06 17:29:03.610: E/AndroidRuntime(1822): java.lang.NullPointerException
12-06 17:29:03.610: E/AndroidRuntime(1822):     at com.google.android.maps.ItemizedOverlay.populate(ItemizedOverlay.java:312)
12-06 17:29:03.610: E/AndroidRuntime(1822):     at com.example.mapproject.OurItemizedOverlay.addOverlay(OurItemizedOverlay.java:108)
12-06 17:29:03.610: E/AndroidRuntime(1822):     at com.example.mapproject.MainActivity.addPointToMap(MainActivity.java:211)

Any ideas? Thanks !


Solution

  • Figured it out, I failed to mention that the PointOfInterest being passed to the addOverlay method was created from a JSON String, therefore the constructor which contains

    super(point, title, snippet)
    

    was never being called.

    I added this to my the addOverlay method which seems to have sorted it !

    protected void addOverlay(PointOfInterest pointOfInterest) {
    
        PointOfInterest pointOfInterest2 = new PointOfInterest(pointOfInterest.title,pointOfInterest.title,pointOfInterest.type,pointOfInterest.point,pointOfInterest.privateField);
        mapOverlays.add(pointOfInterest2);
        setLastFocusedIndex(-1);
        Log.d("Add Overlay", pointOfInterest2.getSnippet());
        populate();
    
    }