I am getting some json from a simple api, to show in a listview.
public class ScenariosActivity extends AppCompatActivity {
private String TAG = MainActivity.class.getSimpleName();
private ListView ScenarioListView ;
ArrayList<HashMap<String, String>> scenarioList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scenarios);
ScenarioListView = findViewById(R.id.scenariosListView);
new getScenarios().execute();
}
@SuppressLint("StaticFieldLeak")
class getScenarios extends AsyncTask<Void, Void, Void>{
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(ScenariosActivity.this,"Json Data is downloading",Toast.LENGTH_LONG).show();
}
@Override
protected Void doInBackground(Void... voids) {
HttpHandler httpHandler = new HttpHandler();
String url = "https://40kapi.evinwijninga.com/scenarios";
String jsonStr = httpHandler.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObject = new JSONObject(jsonStr);
JSONArray scenarios = jsonObject.getJSONArray("scenarios");
Log.e(TAG, "JSONARRAY :"+scenarios);
for (int i = 0; i < scenarios.length(); i++){
Log.e(TAG, "Scenario's length: "+String.valueOf(scenarios.length()));
JSONObject s = scenarios.getJSONObject(i);
String id = null;
String title = null;
if (s.getString("id") != null) {
id = s.getString("id");
Log.e(TAG, id);
} else {
Log.e(TAG, "id = null");
}
if (s.getString("title") != null) {
title = s.getString("title");
Log.e(TAG, title);
} else {
Log.e(TAG, "title = null");
}
if (id != null && title != null){
// make new scenario
HashMap<String, String> scenario = new HashMap<>();
// add properties to scenario
scenario.put("id", id);
scenario.put("title", title);
// add scenario to scenariolist
scenarioList.add(scenario);
} else {
Log.e(TAG, "id or title is null");
}
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG).show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
ListAdapter adapter = new SimpleAdapter(ScenariosActivity.this, scenarioList,
R.layout.scenario_list_item, new String[]{"title"},
new int[]{R.id.title});
ScenarioListView.setAdapter(adapter);
}
}
}
When executed I get a null reference:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
I logged the length of the array, and it is 3, which is fine. I get the id and string of the first jsonobject logged, and that's it.
When looping through the json array for its objects I do something wrong.