1

I have a RecyclerView that gets data from a Firebase database. When the RecyclerView reaches the end of the data feed the app crashes with following:

Logcat file:

 09-27 15:30:12.826 30184-30184/com.pctoolmanconsulting.trackmyweight E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                   Process: com.pctoolmanconsulting.trackmyweight, PID: 30184
                                                                                   java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.startsWith(java.lang.String)' on a null object reference
                                                                                       at com.pctoolmanconsulting.trackmyweight.ProgressViewHolder.bind(ProgressViewHolder.java:40)
                                                                                       at com.pctoolmanconsulting.trackmyweight.ProgressAdapter.onBindViewHolder(ProgressAdapter.java:41)
                                                                                       at com.pctoolmanconsulting.trackmyweight.ProgressAdapter.onBindViewHolder(ProgressAdapter.java:15)

I can not figure out where I am supposed to check for the null value. In the bind method of my ViewHolder class or in the adapter class.

Can someone help point me in the right direction?
Thanks.

My ViewHolder bind method:

public void bind (ImageProgress progress){
  name.setText(progress.name);

  if(progress.progressDetail.startsWith("https://firebasestorage.googleapis.com/") || progress.progressDetail.startsWith("content://")){
    progressDetailMessage.setVisibility(View.INVISIBLE);
    image.setVisibility(View.VISIBLE);
    Glide.with(activity).load(progress.progressDetail).fitCenter().into(image);

  } else {
     progressDetailMessage.setVisibility(View.VISIBLE);
     image.setVisibility(View.GONE);
     progressDetailMessage.setText(progress.progressDetail);
  }
}

adapter class:

public class ProgressAdapter extends RecyclerView.Adapter<ProgressViewHolder> {

  private static final String TAG ="ProgressAdapter";
  private final Activity activity;
  List<ImageProgress> progressDetails = new ArrayList<>();

  public ProgressAdapter(Activity activity){
    this.activity = activity;
  }

  public void addProgress(ImageProgress progress){
    progressDetails.add(progress);
    notifyItemInserted(progressDetails.size());
  }

  @Override
  public ProgressViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
    return new ProgressViewHolder(activity, activity.getLayoutInflater().inflate(android.R.layout.two_line_list_item,
            parent, false));
  }

  @Override
  public void onBindViewHolder(ProgressViewHolder holder, int position){
    holder.bind(progressDetails.get(position));
  }

  @Override
  public int getItemCount() {
    return progressDetails.size();
  }
}
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
hozdaman
  • 319
  • 2
  • 5
  • 15

1 Answers1

3

Just like the other guys said, you should check null first. It occurred that the progressDetail must be null when you scroll to the last item.

Let me tell you how to find out this:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.startsWith(java.lang.String)' on a null object reference

See what the error said is 'Attempt to invoke virtual method "startsWith"', and you may think whose method the 'startsWith' is. Check your code after reading the error message you will notice that 'startsWith' was called on an object 'progressDetail' which was null !!

OK, all clear. You should check null to avoid exception.

BUT!!! THE MOST IMPORTANT IS YOU SHOULD FIND OUT WHY IT CAN BE NULL.

Benny Huo
  • 390
  • 1
  • 2
  • 11