Smart Programmer
Smart Programmer

Reputation: 1

Error occur in recyclerView

This app is getting data directly from API. When it goes to run such type of error occurs. Basically I think the error occurs in the getItemCount() method but i'm not be able to resolve the problem. I've attached the error file and the whole code.I shall be thankful if you can resolve the error.

java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference at com.techbrightsoft.database.MyRecyler.getItemCount(MyRecyler.java:36) at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3603) at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3103) at android.view.View.measure(View.java:20151) at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:934) at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:973) at android.view.View.measure(View.java:20151) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6330) at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139) at android.view.View.measure(View.java:20151) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6330) at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:400) at android.view.View.measure(View.java:20151) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6330) at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) at android.view.View.measure(View.java:20151) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6330) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464) at android.widget.LinearLayout.measureVertical(LinearLayout.java:747) at android.widget.LinearLayout.onMeasure(LinearLayout.java:629) at android.view.View.measure(View.java:20151) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6330) at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3158) at android.view.View.measure(View.java:20151) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2594) at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1549) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1841) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7397) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920) at android.view.Choreographer.doCallbacks(Choreographer.java:695) at android.view.Choreographer.doFrame(Choreographer.java:631) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:7224) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

  public class MyRecyler extends RecyclerView.Adapter<MyRecyler.MyHolder> {
  private Context context;
   private ArrayList<String> names;

    public MyRecyler(Context context, ArrayList<String> names) {
        this.context = context;
        this.names = names;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(this.context);
        View view = inflater.inflate(R.layout.names, parent, false);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        holder.name.setText(this.names.get(position));
    }

    @Override
    public int getItemCount() {
        return names.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        TextView name;

        public MyHolder(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);
        }
    }


    public class MainActivity extends AppCompatActivity {
    public static ArrayList<String> authors_names ;
    RecyclerView recyclerView;
    EditText editText;
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.recyclerView);
        // Downloading Data From Internet
        DownloadingData downloadingData = new DownloadingData();
        downloadingData.execute("https://newsapi.org/v1/articles?source=the-next-web&sortBy=latest&apiKey=a301a9e90c774b4ebf7eb21000cdbdef");


        // RecyclerView Set
        MyRecyler recyler = new MyRecyler(MainActivity.this,authors_names);
        recyclerView.setAdapter(recyler);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyler.notifyDataSetChanged();
    }
}

Upvotes: 0

Views: 612

Answers (1)

Afshin
Afshin

Reputation: 9173

It seems this code does not properly initialize authors_names. If I have guess your code logic correctly, you want to fill authors_names with result from downloadingData. Generally, passing that list to your adapter is a bad practice.

My suggestion is initializing names with an empty list and define a addAll() method for updating its content. A simple and unoptimized implementation will be like this:

private ArrayList<String> names = new ArrayList<>();

public void addAll(ArrayList<String> items) {
    names.clear();
    names.addAll(items);
    notifyDatasetChanged();
}

And you need to add items with addAll() method from main code when downloadingData finished its job(normally through an callback).

Upvotes: 0

Related Questions