Search code examples
androidsqlitenul

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


I having a listview in which i have fetch the values from sqlite the values which i have fetched from sqlite are stored in base adapter where i have used condition to check the strings has values or not but still it shows me the null pointer exception even i have added the not null in the condition but it didn't works and here is my code:

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

    }

Exception:

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)

Solution

  • change your code like this structure

    if(m.getUsertype() != null && !m.getUsertype().isEmpty()) {
    
    
     if (m.getUsertype().startsWith("farmer")
     {
     }
     else if (m.getUsertype().startsWith("advancefarmer")
     {
     }
     ....
     }