0

I am a beginner, and I am trying to use ArrayList from an Interface. I got the structure from an article here. The problem is it's giving me null when I try to use it in a Fragment.

The class is:

public class NewsFRagment extends Fragment implements BackgroundTask.CallBack {

    private ArrayList<NewsPOJO> arrayList1;

    public NewsFRagment() {

        // Required empty public constructor
    }

    @Override
    public void onSuccess(ArrayList<NewsPOJO> arrayList) {

        this.arrayList1 = arrayList;
        Log.d("arraylist in onSuccess", String.valueOf(arrayList.size()));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.news_fragment, container, false);

        BackgroundTask backgroundTask = new BackgroundTask(this.getActivity(), this);
        backgroundTask.execute();

        RecyclerView recyclerView = (RecyclerView) v.findViewById(R.id.rv_news);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        Log.d("Arraylist1 size", String.valueOf(+arrayList1.size()));
        recyclerView.setAdapter(new MyRecyclerAdapter(this.getActivity(), arrayList1));

        return v;
    }

    @Override
    public String toString() {
        return "News Fragment";
    }

}

I need to use arraylist from the onSuccess and pass it in my setAdapter method, Problem is its null when I pass it in setAdaper, but it's not null in the onSuccess, How can I solve it?

This is logcat:

06-03 04:52:50.367 21851-21851/hilz.reycfrag W/art: Failed to find OatDexFile for DexFile /data/data/hilz.reycfrag/files/instant-run/dex/slice-slice_8-classes.dex ( canonical path /data/data/hilz.reycfrag/files/instant-run/dex/slice-slice_8-classes.dex) with checksum 0xd8bc1e5a in OatFile /data/data/hilz.reycfrag/cache/slice-slice_8-classes.dex
06-03 04:52:51.150 21851-21851/hilz.reycfrag D/AndroidRuntime: Shutting down VM
06-03 04:52:51.150 21851-21851/hilz.reycfrag E/AndroidRuntime: FATAL EXCEPTION: main
Process: hilz.reycfrag, PID: 21851
java.lang.RuntimeException: Unable to start activity ComponentInfo{hilz.reycfrag/hilz.reycfrag.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at hilz.reycfrag.NewsFRagment.onCreateView(NewsFRagment.java:45)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:330)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:547)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1236)
at android.app.Activity.performStart(Activity.java:6006)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
06-03 04:52:51.178 21851-21897/hilz.reycfrag D/arraylist in onSuccess: 4
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
The_Hilz
  • 33
  • 1
  • 8

2 Answers2

0

Check in what order the callbacks happen...

Your arraylist may not be initialized, when the callback happens at setAdapter as you only declare it. You never initialize it in constructor. As opposed to in the OnSuccess call back, where you receive an arraylist reference as input and set your local instance variable to it.

The creation of the view would be the initial callback run, so if you need to pass the list you need to initialise it first, you could for instance add an initialization to the constructor:

public NewsFRagment() {

    // Required empty public constructor
    arrayList1 = new ArrayList<NewsPOJO>();

}

Or you could have a specific initialise method, that you call after doing needed stuff in the view creation event first (if you need to pre-set the list).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Richard Tyregrim
  • 582
  • 2
  • 12
0
@Override
public void onSuccess(ArrayList<NewsPOJO> arrayList) {

    this.arrayList1 = arrayList;
    Log.d("arraylist in onSuccess", String.valueOf(arrayList.size()));

    // Make sure your fragment is done view loading before on success.
    RecyclerView recyclerView = (RecyclerView) v.findViewById(R.id.rv_news);
    recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
    Log.d("Arraylist1 size", String.valueOf(+arrayList1.size()));
    recyclerView.setAdapter(new MyRecyclerAdapter(this.getActivity(), arrayList1));

}

Call the below code on start, show a progress dialog, and cancel on success:

BackgroundTask backgroundTask = new BackgroundTask(this.getActivity(), this);
backgroundTask.execute();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sush
  • 3,864
  • 2
  • 17
  • 35