Showing Error :Attempt to invoke interface method 'int java.util.List.size()' on a null object reference

前端 未结 3 906
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-19 20:42

I\'m creating an app that display contact list in a listview.The problem is I\'m getting the following error when I run my app and I\'m struggling to fix it:



        
相关标签:
3条回答
  • 2020-12-19 21:18

    Extract this line

    List<ContactModel> contactModelList = new ArrayList<>();
    

    before the try block and instead of return null; use return contactModelList;

    Looks like your parsing fails and you pass a null object to onPostExecute().

    0 讨论(0)
  • 2020-12-19 21:20

    The List object which you passed into the adapter is null. So when the adapter calls list.Size(), a null pointer exception is thrown.

    Use a debugger, set a breakpoint somewhere around com.kksworld.jsonparsing.MainActivity$JSONTask.onPostExecute(MainActivity.java:48), and work your way backward from there if you can't figure it out.

    One assumes that your json parse failed somehow or another.

    0 讨论(0)
  • 2020-12-19 21:22

    I was building similar app from youtube video and I've got same error, but I saw also error "Cleartext HTTP traffic not allowed to site.com", so I found solution android:usesCleartextTraffic="true" here Android 8: Cleartext HTTP traffic not permitted and it solved all problems without modifying MainActivity. Btw, on API 16 it wasn't showing errors.

    0 讨论(0)
提交回复
热议问题