我有一个listview,其中我从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)
答案 0 :(得分:0)
像这样的结构改变你的代码
if(m.getUsertype() != null && !m.getUsertype().isEmpty()) {
if (m.getUsertype().startsWith("farmer")
{
}
else if (m.getUsertype().startsWith("advancefarmer")
{
}
....
}