Reputation: 325
I am building an Android app where I download a XML file containing content to display on a GoogleMap. The info-windows are clickable, so to differentiate between the different windows I use a Hashmap.
Whenever the map loads it crashes with a NullPointerException. The hashmap is named markerEntryHashMap, here's some code:
private class DownloadXmlTask extends AsyncTask<String, Void, String> {
/**
* Hashmap with marker; String ids as key, entry obj as value
*/
private HashMap<String, CurvitVacatureXmlParser.Entry> markerEntryHashMap;
@Override
protected String doInBackground(String... urls) {
[...]
}
@Override
protected void onPostExecute(String result) {
for (CurvitVacatureXmlParser.Entry entry : mapEntries) {
[...]
Log.d(TAG, "Marker ID: " + m.getId()); // OK; Marker ID: m0
Log.d(TAG, "Entry ID: " + entry.id); // OK; Entry ID: 12
// there is no unique identifier in an info-window so I thought using the hashmap to differentiate later on in an OnInfoWindowClickListener
// This crashes: FATAL EXCEPTION: main java.lang.NullPointerException
markerEntryHashMap.put(m.getId(), entry);
}
}
}
Any of you guys/gals spot the mistake?
Upvotes: 0
Views: 95
Reputation: 621
You have not initialized a HashMap , You can initialize it in onPreExecute or onPostExecute before for loop as below:
private class DownloadXmlTask extends AsyncTask<String, Void, String> {
/**
* Hashmap with marker; String ids as key, entry obj as value
*/
private HashMap<String, CurvitVacatureXmlParser.Entry> markerEntryHashMap;
@Override
protected String doInBackground(String... urls) {
[...]
}
@Override
protected void onPostExecute(String result) {
markerEntryHashMap=new HashMap<String,CurvitVacatureXmlParser.Entry>();
for (CurvitVacatureXmlParser.Entry entry : mapEntries) {
[...]
Log.d(TAG, "Marker ID: " + m.getId()); // OK; Marker ID: m0
Log.d(TAG, "Entry ID: " + entry.id); // OK; Entry ID: 12
// there is no unique identifier in an info-window so I thought using the hashmap to differentiate later on in an OnInfoWindowClickListener
// This crashes: FATAL EXCEPTION: main java.lang.NullPointerException
markerEntryHashMap.put(m.getId(), entry);
}
}
}
Upvotes: 1
Reputation: 152
You have to initialize your HashMap
Change
private HashMap<String, CurvitVacatureXmlParser.Entry> markerEntryHashMap;
to
private HashMap<String, CurvitVacatureXmlParser.Entry> markerEntryHashMap = new HashMap<>();
Upvotes: 3