Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – RecyclerView adapter giving null exception inside Fragment

Posted on November 14, 2022

Solution 1 :

Maybe because you are setting the recyclerview adapter two times, one in the onCreateView and another in the sendData method. Try removing the first one and leave the other of sendData.

Try leaving the admissionRecycler.setAdapter(aAdapter); of your onCreateView, delete the other one and after aAdapter.setJsonData(admitList); put aAdapter.notifyDataSetChanged();

Problem :

In my app, I am using android’s Tabbed activity. I am getting data from network and want to distribute in two tabs. I am making my network call inside my main activity and send those data as json string to both tab which I am going to display in Recyclerview. I am using an interface to send data to both tabs and data passing without any problem. My problem is I am getting a NullPointerException from the adapter which is inside Fragment.

Activity

public class MyHistoryTabbedActivity extends AppCompatActivity {

    public SendDataInterfaceTab1 sdInfaceOne;
    public SendDataInterfaceTab2 sdInfaceTwo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_history_tabbed);

        ViewPager viewPager = findViewById(R.id.view_pager);
        TabLayout tabs = findViewById(R.id.tabs);
        SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
        viewPager.setAdapter(sectionsPagerAdapter);
        tabs.setupWithViewPager(viewPager);

        sdInfaceOne = new AdmissionFragment();
        sdInfaceTwo = new DischargeFragment();

...

        getHistoryList(userId, sFromDate, sToDate);

      ...

    }

    private void getHistoryList(String userId, String fDate, String tDate) {

      
      
            StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_MY_HISTORY, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    try {
                        JSONObject jObj = new JSONObject(response);

                        if (jObj.getString("authStatus").equals("1")) {

                            String admitJson = jObj.getString("admitList");
                            String dischJson = jObj.getString("dischList");

                            sdInfaceOne.sendData(admitJson);
                            sdInfaceTwo.sendData(dischJson);

                        } else {

                            Toast.makeText(getApplicationContext(), jObj.getString("authMesg"), Toast.LENGTH_SHORT).show();

                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }) {



            };

    
    }

    public interface SendDataInterfaceTab1 {
        void sendData(String message);
    }

    public interface SendDataInterfaceTab2 {
        void sendData(String message);
    }
}

AdmissionFragment

public class AdmissionFragment extends Fragment implements MyHistoryTabbedActivity.SendDataInterfaceTab1 {

  private RecyclerView admissionRecycler;
    private AdmissionAdapter aAdapter;

    public AdmissionFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_admission, container, false);

        admissionRecycler = view.findViewById(R.id.admissionRecycler);

        Context context = view.getContext();

        aAdapter = new AdmissionAdapter(context);
        admissionRecycler.setLayoutManager(new LinearLayoutManager(context));
        admissionRecycler.setAdapter(aAdapter);

        return view;

    }

    @Override
    public void sendData(String message) {

        if (message != null) {

            Gson gson = new Gson();
            ArrayList<MyHistoryData> admitList = gson.fromJson(message, new TypeToken<ArrayList<MyHistoryData>>() {
            }.getType());

            Log.d("admitList", String.valueOf(admitList.size()));

            if (admitList.size() > 0) {
                aAdapter.setJsonData(admitList);
                admissionRecycler.setAdapter(aAdapter);
            }

        }

    }

}

I am getting error from this line aAdapter.setJsonData(admitList);

READ  [ANSWERED] java - Flutter, Accessing hidden method
Powered by Inline Related Posts

LogCat

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.slic.suraksha.adapter.AdmissionAdapter.setJsonData(java.util.ArrayList)' on a null object reference
        at com.slic.suraksha.fragment.AdmissionFragment.sendData(AdmissionFragment.java:106)
        at com.slic.suraksha.activities.MyHistoryTabbedActivity$3.onResponse(MyHistoryTabbedActivity.java:199)
        at com.slic.suraksha.activities.MyHistoryTabbedActivity$3.onResponse(MyHistoryTabbedActivity.java:187)
        at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:82)
        at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:29)
        at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7266)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress