0

Working with the Google Maps API for Android (v2), I am trying to store some country Polygon approximations into a data structure, particularly, a map with String as keys (name of the country) and an ArrayList of PolygonOptions.

This var is private and declared by:

private Map<String, ArrayList<PolygonOptions>> worldMap;

But my code crashes when I try to fill it with a PolygonOption, in the following function:

private void addSomeCountry() {
        ArrayList <PolygonOptions> MyPoli = new ArrayList<PolygonOptions>();
        PolygonOptions rectOptions = new PolygonOptions()
                .add(new LatLng(42.569962, 1.78172 ))
                .add(new LatLng(42.509438, 1.723611))  
                .add(new LatLng(42.601944, 1.445833))  
                .add(new LatLng(42.569962, 1.78172)) 
                .fillColor(Color.TRANSPARENT)
                .strokeColor(Color.CYAN);
        MyPoli.add(rectOptions);
        worldMap.put("Andorra", MyPoli);
    }

Specifically, it crashes with the following error:

04-04 21:20:30.335  22562-22562/otorrillas.geography E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: otorrillas.geography, PID: 22562
Caused by: java.lang.NullPointerException
             at otorrillas.geography.DataSaver.addSomeCountry(DataSaver.java:49)
             at otorrillas.geography.DataSaver.onCreate(DataSaver.java:32)

Am I doing something wrong?

1 Answer 1

2

I don't see you initializing worldMap anywhere.

private Map<String, ArrayList<PolygonOptions>> worldMap = new HashMap<>();

should do.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.