0

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)
Noobprogrammer
  • 67
  • 2
  • 10
  • so jsonArray is not null? would you show the error log? – calvinfly Nov 20 '15 at 02:23
  • You need to the error log, and indicate what line is causing the exception – Joel Min Nov 20 '15 at 03:02
  • added the error log. @Calvinfly i believe the jsonArray when called in the for loop is null but I don't really know how to change that since its meant to be a placeholder here and the method its in is eventually meant to be run with a real value in GetAllCustomerTask. Joel, the primary error at least as i see it is in the for loop line – Noobprogrammer Nov 20 '15 at 03:05
  • I think `params[0].GetAllCustomers();` returns null – BNK Nov 20 '15 at 03:51
  • @Noobprogrammer ApartmentInventory.java line 118 is the for loop ? check `params[0].GetAllCustomers()` whether is null or not – calvinfly Nov 20 '15 at 04:56
  • @calvinfly, yes that line is the for loop. i'll look into the params line some more – Noobprogrammer Nov 20 '15 at 04:58
  • i guess what i'm really struggling with is i can do something like this to initialize jsonarray before i do my for loop but then i get a networkingonmainthreaderror. In other words, isn't that the whole point of having a separate class that runs all that code. how else do i initialize my jsonArray here: ApiConnector getApiJson = new ApiConnector(); jsonArray = getApiJson.GetAllCustomers(); For the params line my understanding is that returns the first parameter from my AsyncTask, in this case I'm effectively calling ApiConnector.GetAllCustomers(). Do I understand that right? – Noobprogrammer Nov 20 '15 at 22:19
  • hey guys, solved the nullpointer error (tho still having issues with the broader jsonarray). seems the jsonarray wasn't coming in right even though it was coming in in my log check. i changed how i went about it and moved away from code based on an older tutorial i was following and now it seems to be coming in. i'll open up a new question for the new issue. thanks for the help guys – Noobprogrammer Nov 21 '15 at 21:19

0 Answers0