-2

Everytime I try to run my app, it runs but then it crashes and I get the same message in my logcat. The odd thing is the 2 lines (I added comments in the code where the lines are it's referring to). it refers to when it crashes I haven't touched them. That's how the code has always been, as i inherited working on this app and that's how it's always been. Here is the logcat message:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.example.gabe.politicianspulse.NewsRvAdapter.onBindViewHolder(NewsRvAdapter.java:43)
        at com.example.gabe.politicianspulse.NewsRvAdapter.onBindViewHolder(NewsRvAdapter.java:19)
        at

public class NewsRvAdapter extends RecyclerView.Adapter<NewsRvAdapter.MyViewHolder> { //this is line 19
private Context context;
private List<NewsItem> newsList;

//default constructor
public NewsRvAdapter(){}

public NewsRvAdapter(List<NewsItem> newsList, Context context){
    this.newsList = newsList;
    this.context = context;
}
@NonNull
@Override
public NewsRvAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view;
    LayoutInflater layoutInflater = LayoutInflater.from(context);
    view = layoutInflater.inflate(R.layout.news_card, viewGroup, false);
    final MyViewHolder viewHolder = new MyViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull final NewsRvAdapter.MyViewHolder myViewHolder, int i) {
    myViewHolder.title.setText(newsList.get(i).getTitle());

   myViewHolder.description.setText(newsList.get(i).getDescription());//this line 43 

    myViewHolder.date.setText(newsList.get(i).getDate());

    //Load image URL using Picasso
    Picasso.get()
            .load(newsList.get(i).getImageUrl())
            .centerCrop()
            .fit()
            .into(myViewHolder.photoUrl);

    //on click listener that opens url in webview
    myViewHolder.photoUrl.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            String link = newsList.get(myViewHolder.getAdapterPosition()).getLink();
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.addCategory(Intent.CATEGORY_BROWSABLE);
            intent.setData(Uri.parse(link));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    });

    //onclick that opens new activity
    myViewHolder.title.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            Intent i = new Intent(context, SingleNews.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.putExtra("title", newsList.get(myViewHolder.getAdapterPosition()).getTitle());
            i.putExtra("urlToImage", newsList.get(myViewHolder.getAdapterPosition()).getImageUrl());
            i.putExtra("publishedAt", newsList.get(myViewHolder.getAdapterPosition()).getDate());
            i.putExtra("content", newsList.get(myViewHolder.getAdapterPosition()).getContent());
            context.startActivity(i);
        }
    });
}

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

public static class MyViewHolder extends RecyclerView.ViewHolder{
    TextView title, description, date;
    ImageView photoUrl;
    LinearLayout viewContainer;
    public MyViewHolder(@NonNull View itemView) {
        super(itemView);
        title = itemView.findViewById(R.id.news_title);
        date = itemView.findViewById(R.id.news_date);
        //description = itemView.findViewById(R.id.news_description);
        photoUrl = itemView.findViewById(R.id.news_photo);
        viewContainer = itemView.findViewById(R.id.newsView_container);
    }
}

}

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
  • You commented out the line that assigns `description` in the `MyViewHolder` constructor, so it's still null when you call `setText()` on it in `onBindViewHolder()`. – Mike M. Aug 24 '19 at 18:48
  • Are you sure about this line in the constructor of MyViewHolder? `//description`. In this way it is `null` – Gabriele Mariotti Aug 24 '19 at 19:53

1 Answers1

-1

you commented the description textView intialization on your MyViewHolder constructor so you must uncomment it to avoid the nullPointerExeption when you call setText ()

Achref ArShavin
  • 219
  • 3
  • 10