Trying to call the virtual method "int java.lang.String.length ()" on a null object reference

So I have an Android app with tabs and RecyclerView

. When I run my application, it crashes.

LogCat:

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
        at java.io.StringReader.<init>(StringReader.java:47)
        at android.text.HtmlToSpannedConverter.convert(Html.java:442)
        at android.text.Html.fromHtml(Html.java:136)
        at android.text.Html.fromHtml(Html.java:99)
        at com.example.app.MyRecyclerViewAdapter2.onBindViewHolder(MyRecyclerViewAdapter2.java:80)
        at com.example.app.MyRecyclerViewAdapter2.onBindViewHolder(MyRecyclerViewAdapter2.java:17)
       ...

      

at com.example.app.MyRecyclerViewAdapter2.onBindViewHolder (MyRecyclerViewAdapter2.java:80)

It points to this line of code

listRowViewHolder.postId.setText(Html.fromHtml(listItems.getPostId())); // This line of code leads to crashing of app

      

If I remove this line, my application works fine.

MyRecyclerViewAdapter2.java:

    package com.example.app;


import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;

import java.util.List;

public class MyRecyclerViewAdapter2 extends RecyclerView.Adapter<ListRowViewHolder> {
    private List<ListItems> listItemsList;
    private Context mContext;
    private ImageLoader mImageLoader;
    private int focusedItem = 0;

public MyRecyclerViewAdapter2(Context context, List<ListItems> listItemsList) {
    this.mContext = context;
    this.listItemsList = listItemsList;
}

@Override
public ListRowViewHolder onCreateViewHolder(final ViewGroup viewGroup, int position) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_view_row2, null);

    ListRowViewHolder holder = new ListRowViewHolder(v);

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TextView postUrl =  (TextView) v.findViewById(R.id.postId);
            String post_Id = postUrl.getText().toString();

            Intent intent = new Intent(mContext, ViewPostActivity.class);
            intent.putExtra("id", post_Id);
            mContext.startActivity(intent);

        }
    });

    return holder;
}

@Override
public void onBindViewHolder(ListRowViewHolder listRowViewHolder, int position) {
    ListItems listItems = listItemsList.get(position);
    listRowViewHolder.itemView.setSelected(focusedItem == position);

    listRowViewHolder.getLayoutPosition();

    mImageLoader = MySingleton.getInstance(mContext).getImageLoader();
    listRowViewHolder.thumbnail.setImageUrl(listItems.getThumbnail(), mImageLoader);
    listRowViewHolder.thumbnail.setDefaultImageResId(R.drawable.website_placeholder);

    listRowViewHolder.title.setText(Html.fromHtml(listItems.getTitle()));
    listRowViewHolder.date.setText(Html.fromHtml(listItems.getDate()));
    listRowViewHolder.content.setText(Html.fromHtml(listItems.getContent()));
    listRowViewHolder.postId.setText(Html.fromHtml(listItems.getPostId())); // This line of code leads to crashing of app
}

public void clearAdapter () {
    listItemsList.clear();
    notifyDataSetChanged();
}


@Override
public int getItemCount() {
    return (null != listItemsList ? listItemsList.size() : 0);
}}

      

+3


source to share


1 answer


Your error because it means you are calling Html.fromHtml

on an empty string. You must check the value listItems.getPostId()

before calling Html.fromHtml

.



0


source







All Articles