Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference [duplicate]

こ雲淡風輕ζ 提交于 2019-12-10 09:58:18

问题


So I have Android app with Tabs and RecyclerView. When I run my app, 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 the code

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

If I delete this line, my app will work 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);
}}

回答1:


Your error because means that you call Html.fromHtml on a null string. You should test the value of listItems.getPostId() before calling Html.fromHtml.



来源:https://stackoverflow.com/questions/31243401/attempt-to-invoke-virtual-method-int-java-lang-string-length-on-a-null-objec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!