really struggling connecting my Android app to an API that has the JSON I need. My log shows the JSON comes in (i.e. I can see the full JSON array in Android Studio's log) but I'm getting a nullpointer error in my for loop line. I know I'm supposed to initialize it first but my JSONArray is really a placeholder until the getApartmentList method is called in another class. And directly trying to reach the API to give the JSONArray a real value here obviously results in a networkonmainthread error. I'm probably overcomplicating this but what am I missing?
public List<Apartment> getApartmentList(JSONArray jsonArray) { //added the parameter, placeholder for "apartments" array which contains all apts in our database
List<Apartment> apartmentList = new ArrayList<>();
for(int i=0; i<jsonArray.length();i++){ //nullpointer exception here
JSONObject json = null;
try {
json = jsonArray.getJSONObject(i);
Apartment apartment = new Apartment(UUID.fromString(json.getString("id") ) ); //UUID from String already converts our JSON string result into a UUID
apartment.setApartmentText(json.getInt("price") + " " + json.getInt("bedrooms") + " " + json.getInt("bathrooms") );
apartment.setApartmentLatitude(json.getJSONObject("building").getDouble("latitude"));
apartment.setApartmentLongitude(json.getJSONObject("building").getDouble("longitude"));
apartmentList.add(apartment);
} catch (JSONException e) {
e.printStackTrace();
}
}//end of for loop
return apartmentList;
}//end of getApartmentList method
This is where this method is called in a separate class and gets a real value:
public class GetAllCustomerTask extends AsyncTask<ApiConnector,Long,JSONArray> {
@Override
protected JSONArray doInBackground(ApiConnector... params) {
return params[0].GetAllCustomers();
}
//the ApiConnector class is where I connect to the URL of the API, i.e. where i connect to the JSON
@Override
protected void onPostExecute(JSONArray jsonArray) {
ApartmentInventory apartmentInventory = new ApartmentInventory(getActivity() );
apartmentInventory.getApartmentList(jsonArray);
}
} // end of GetAllCustomerTask method
EDIT - pasting log
11-19 22:00:22.787 24528-24528/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example, PID: 24528
java.lang.NullPointerException
at ApartmentInventory.getApartmentList(ApartmentInventory.java:118)
at ListingsFragment$GetAllCustomerTask.onPostExecute(ListingsFragment.java:79)
at ListingsFragment$GetAllCustomerTask.onPostExecute(ListingsFragment.java:54)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5487)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)