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);
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)