0

我有一个列表视图,我在其中从 sqlite 获取值我从 sqlite 获取的值存储在基本适配器中,我使用条件检查字符串是否有值,但它仍然向我显示空指针异常,即使我在条件中添加了 not null 但它不起作用,这是我的代码:

 final Daybooklist m = daybooklists.get(position);
    if (m.getUsertype().startsWith("farmer") && m.getUsertype() != null) {
        day_name.setText(m.getName());
        day_description.setText(m.getDescription());
        day_type.setText(m.getType());
        if (m.getName().startsWith("no") && m.getName() != null) {
            day_name.setText(" ");
        } else if (m.getDescription().startsWith("no") && m.getDescription() != null) {
            day_description.setText(" ");
        }
        day_amount.setText("\u20B9" + m.getAmountin());

    } else if (m.getUsertype().startsWith("advancefarmer") && m.getUsertype() != null) {
        day_name.setText(m.getName());
        day_description.setText(m.getDescription());
        day_type.setText(m.getType());
        if (m.getName().startsWith("no") && m.getName() != null) {
            day_name.setText(" ");
        } else if (m.getDescription().startsWith("no") && m.getDescription() != null) {
            day_description.setText(" ");
        }
        Log.e("amountout",m.getAmountout());
        day_amount.setText("\u20B9" + m.getAmountout());
    } else {
        day_name.setText(m.getName());
        day_description.setText(m.getDescription());
        day_type.setText(m.getType());
        if (m.getName().startsWith("no") && m.getName()!=null) {
            day_name.setText(" ");
        } else if (m.getDescription().startsWith("no") && m.getDescription() != null) {
            day_description.setText(" ");
        }
        Log.e("amountout",m.getAmountout());
        day_amount.setText("\u20B9" + m.getAmountout());

    }

例外:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.startsWith(java.lang.String)' on a null object reference
                                                                    at codingtown.coconut.daybook.adapter.Daybooklist_adapter.getView(Daybooklist_adapter.java:96)
                                                                    at android.widget.AbsListView.obtainView(AbsListView.java:2360)
                                                                    at android.widget.ListView.measureHeightOfChildren(ListView.java:1270)
                                                                    at android.widget.ListView.onMeasure(ListView.java:1182)
                                                                    at codingtown.coconut.libraries.expandablelistview.ExpandableHeightListView.onMeasure(ExpandableHeightListView.java:34)
4

1 回答 1

0

像这样的结构改变你的代码

if(m.getUsertype() != null && !m.getUsertype().isEmpty()) {


 if (m.getUsertype().startsWith("farmer")
 {
 }
 else if (m.getUsertype().startsWith("advancefarmer")
 {
 }
 ....
 }
于 2016-11-17T05:05:06.723 回答