0

Whenever I click to the news it crashes, below is LOGCAT

java.lang.NullPointerException: Attempt to invoke virtual method 'long java.util.Date.getTime()' on a null object reference at com.jimdo.saifstudios.lynknews.Adapter.ListNewsAdapter.onBindViewHolder(ListNewsAdapter.java:105) at com.jimdo.saifstudios.lynknews.Adapter.ListNewsAdapter.onBindViewHolder(ListNewsAdapter.java:70)

ListNewsAdapter

class ListNewsViewHolder extends RecyclerView.ViewHolder implements 
View.OnClickListener
{

ItemClickListener itemClickListener;

TextView article_title;
RelativeTimeTextView article_time;
CircleImageView article_image;

public ListNewsViewHolder(View itemView) {
    super(itemView);
    article_image = 
(CircleImageView)itemView.findViewById(R.id.article_image);
    article_title = (TextView) itemView.findViewById(R.id.article_title);
    article_time = (RelativeTimeTextView) 
itemView.findViewById(R.id.article_time);

    itemView.setOnClickListener(this);
}

public void setItemClickListener(ItemClickListener itemClickListener) {
    this.itemClickListener = itemClickListener;
}

public void setArticle_title(TextView article_title) {
    this.article_title = article_title;
}

public void setArticle_time(RelativeTimeTextView article_time) {
    this.article_time = article_time;
}

public void setArticle_image(CircleImageView article_image) {
    this.article_image = article_image;
}

@Override
public void onClick(View view) {
    itemClickListener.onClick(view,getAdapterPosition(),false);

}
}

public class ListNewsAdapter extends 
RecyclerView.Adapter<ListNewsViewHolder> 
{
private List<Article> articleList;
private Context context;

public ListNewsAdapter(List<Article> articleList, Context context) {
    this.articleList = articleList;
    this.context = context;
}

@Override
public ListNewsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
{
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View itemView = inflater.inflate(R.layout.news_layout,parent,false);
    return new ListNewsViewHolder(itemView);
}

@Override
public void onBindViewHolder(ListNewsViewHolder holder, int position) {

    Picasso.with(context)
            .load(articleList.get(position).getUrlToImage())
            .into(holder.article_image);
    if (articleList.get(position).getTitle().length() > 65)
     holder.article_title.setText(articleList.get(position).getTitle().substring(0,65)+"...");
    else
        holder.article_title.setText(articleList.get(position).getTitle());

    Date date=null;
    try
    {
        date = ISO8601Parse.parse(articleList.get(position).getPublishedAt());
    }catch (ParseException ex)
    {
        ex.printStackTrace();
    }
    holder.article_time.setReferenceTime(date.getTime());

    // Set event click
    holder.setItemClickListener(new ItemClickListener() {
        @Override
        public void onClick(View view, int position, boolean isLongClick) {
            Intent detail = new Intent(context,DetailArticle.class);
            detail.putExtra("webURL",articleList.get(position).getUrl());
            detail.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(detail);



        }
    });

}

@Override
public int getItemCount() {
    return articleList.size();
}
}
Saif
  • 23
  • 1
  • 6
  • So where are you using getTime()? And what would then be null? And if there is a catch... Should you neglect it? Or what? Inform the user maybe? And is it clever then to continue as if there had been no catch? – greenapps Oct 20 '17 at 14:51

2 Answers2

0

Your date object is null. You are doing date.getTime() without checking if it is null. Move your holder.article_time.setReferenceTime(date.getTime) into the try block below the date and you should no longer crash.

Then you need to figure out what is wrong with how you are parsing the date to begin with.

Sam
  • 5,342
  • 1
  • 23
  • 39
  • Still getting the same problem – Saif Dec 19 '17 at 15:24
  • You waited a month to reply lol, must not be too big of a priority. You only have one line of code that has date pulled. ava.lang.NullPointerException: Attempt to invoke virtual method 'long java.util.Date.getTime()' that could cause this error. Please supply your updated code showing that you did my suggestion and then supply your updated error. If you put it in a try/catch block then it is not possible to crash as the "catch" would catch it, unless you are crashing in other class that you did not share. So update your question if it is a priority for you and i'm happy to help – Sam Dec 19 '17 at 18:49
  • Sorry I had my exams so I didn't opened my work. and I'm a beginner. this is the android studio project link ( https://drive.google.com/open?id=1ZKyM5yZM017iSEA8jNWP3ejwhAuNRDEd ) – Saif Dec 23 '17 at 09:42
0

you have a problem at

holder.article_time.setReferenceTime(date.getTime());

the date variable still null it is not initialized so you have problem at

ISO8601Parse.parse(articleList.get(position).getPublishedAt());

it throws ParseException check it.

Oussema Aroua
  • 5,225
  • 1
  • 24
  • 44