尝试在空对象引用上调用虚拟方法“int java.lang.String.length()”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31243401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:20:07  来源:igfitidea点击:

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

javaandroidandroid-adapter

提问by Personal Jesus

So I have Android app with Tabs and RecyclerView. When I run my app, it crashes.

所以我有带有 Tabs 和RecyclerView. 当我运行我的应用程序时,它崩溃了。

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)

在 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:

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);
}}

回答by sonic

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

你的错误因为意味着你调用Html.fromHtml了一个空字符串。您应该listItems.getPostId()在调用 之前测试 的值Html.fromHtml