在 android 中将数据从 sqlite 数据库加载到 baseadapter 时出现空指针异常

Getting null pointer exception when loading data from sqlite database to baseadapter in android

我有一个列表视图,其中我从 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)

像这样改变你的代码结构

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


 if (m.getUsertype().startsWith("farmer")
 {
 }
 else if (m.getUsertype().startsWith("advancefarmer")
 {
 }
 ....
 }