0

I am developing new and I am reading JSON file from asset folder but I am getting the following exception

java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
    at yodgorbek.komilov.runtastictask1.CustomAdapter.getView(CustomAdapter.kt:45)
    at android.widget.AbsListView.obtainView(AbsListView.java:2365)
    at android.widget.ListView.makeAndAddView(ListView.java:2052)
    at android.widget.ListView.fillDown(ListView.java:786)
    at android.widget.ListView.fillFromTop(ListView.java:847)
    at android.widget.ListView.layoutChildren(ListView.java:1826)
    at android.widget.AbsListView.onLayout(AbsListView.java:2164)
    at android.view.View.layout(View.java:19828)
    at android.view.ViewGroup.layout(ViewGroup.java:6154)
    at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
    at android.view.View.layout(View.java:19828)
    at android.view.ViewGroup.layout(ViewGroup.java:6154)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:19828)
    at android.view.ViewGroup.layout(ViewGroup.java:6154)
    at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:446)
    at android.view.View.layout(View.java:19828)
    at android.view.ViewGroup.layout(ViewGroup.java:6154)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:19828)
    at android.view.ViewGroup.layout(ViewGroup.java:6154)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
    at android.view.View.layout(View.java:19828)
    at android.view.ViewGroup.layout(ViewGroup.java:6154)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at com.android.internal.policy.DecorView.onLayout(DecorView.java:765)
    at android.view.View.layout(View.java:19828)
    at android.view.ViewGroup.layout(ViewGroup.java:6154)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2643)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2348)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1482)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7124)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1008)
    at android.view.Choreographer.doCallbacks(Choreographer.java:804)
    at android.view.Choreographer.doFrame(Choreographer.java:732)
    at android.view.Choreographer$FrameHandler.handleMessage(Choreographer.java:907)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:176)
    at android.app.ActivityThread.main(ActivityThread.java:6662)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

below my code to gist

8
  • please post your json and pojo class as well Commented Oct 21, 2019 at 12:49
  • possible duplicate stackoverflow.com/questions/218384/… Commented Oct 21, 2019 at 12:49
  • 3
    Possible duplicate of What is a NullPointerException, and how do I fix it? Commented Oct 21, 2019 at 12:50
  • @FurqanKhan My code very long thats why I have posted gistlist please check it stackoverflow did not allowed to post all thats why I shared as gist Commented Oct 21, 2019 at 12:53
  • @sashabeliy check model value is it null? val model = gson.fromJson(loadJSONFromAssets(), NetworkResponse::class.java) Commented Oct 21, 2019 at 12:55

2 Answers 2

1

You are using a wrong key for members in your Group data class, it is data while in your json it shows the key is members use the following class and it will work

data class Group(
    @SerializedName("group_id")
    val group_id: Int,

    @SerializedName("group_name")
    val group_name: String,

    @SerializedName("members")
    val members: List<Member>

)
Sign up to request clarification or add additional context in comments.

4 Comments

it is showing empty white screen what is your suggestion
because in your main activity xml you have not assinged the constraints to list view, i would recommend to change constraint layout to linear layout
it is still the same i have changed to linearlayout vertical
what do you think main problem
0

You are not using correct key

data class Group(
@SerializedName("group_id")
val group_id: Int,

@SerializedName("group_name")
val group_name: String,

@SerializedName("data")
val members: List<Member>)

According to your json schema it should be

data class Group(
@SerializedName("group_id")
val group_id: Int,

@SerializedName("group_name")
val group_name: String,

@SerializedName("members")
val members: List<Member>)

2 Comments

it did not help
App is still crashing or some other error is coming?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.