test123123 test123123 - 1 year ago 69
Android Question

NullPointerException in ListView

Hy!!

I have a listview that should be filled with data from the database

Code:

private void onCreateDBAndDBTabled() {
myDB = this.openOrCreateDatabase(MY_DB_NAME, MODE_PRIVATE, null);
myDB.execSQL("CREATE TABLE IF NOT EXISTS " + MY_DB_TABLE
+ " (_id integer primary key autoincrement, name varchar(100), rate integer(1), eattime datetime)"
+";");
ArrayList<Pizza> list = new ArrayList<Pizza>();
Cursor cursor = this.myDB.query(MY_DB_TABLE, new String[] { "name" },null,null,null,null,null,null);
if (cursor.moveToFirst()) {
do {
Log.e("XXX", "Courser Enter: " + cursor.getString(0));
Pizza pizza = new Pizza();
pizza.title= cursor.getString(0);
list.add(pizza);
}
while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
Log.e("XXX", "Count:" + list.size());
Log.e("XXX", "Item[0] -->" + list.toArray()[0].toString());
CustomAdapter aa = new CustomAdapter(this, R.layout.customlistitem,list);
Log.e("XXX", String.valueOf(aa.getCount()));
lv.setAdapter(aa);
}


Customadapter:

public class CustomAdapter extends ArrayAdapter<Pizza>{

private ArrayList<Pizza> pizzen;

public CustomAdapter(Context context, int textViewResourceId,ArrayList<Pizza> objects) {
super(context, textViewResourceId, objects);
this.pizzen = objects;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
Pizza pizza = pizzen.get(position);
if (v == null) {

}

return v;//super.getView(position, convertView, parent);


}


Pizza:

public class Pizza{

public String title;
public int rate;
public Date date;
}


Error:

09-30 08:58:38.661: ERROR/XXX(667): Courser Enter: EditText
09-30 08:58:38.671: ERROR/XXX(667): Count:1
09-30 08:58:38.671: ERROR/XXX(667): Item[0] -->com.korn.pizzacounter.Pizza@43b91850
09-30 08:58:38.671: ERROR/XXX(667): 1
09-30 08:58:38.751: ERROR/AndroidRuntime(667): Uncaught handler: thread main exiting due to uncaught exception
09-30 08:58:38.762: ERROR/AndroidRuntime(667): java.lang.NullPointerException
09-30 08:58:38.762: ERROR/AndroidRuntime(667): at android.widget.AbsListView.obtainView(AbsListView.java:1276)
09-30 08:58:38.762: ERROR/AndroidRuntime(667): at android.widget.ListView.makeAndAddView(ListView.java:1668)
09-30 08:58:38.762: ERROR/AndroidRuntime(667): at android.widget.ListView.fillDown(ListView.java:637)
09-30 08:58:38.762: ERROR/AndroidRuntime(667): at android.widget.ListView.fillFromTop(ListView.java:694)
09-30 08:58:38.762: ERROR/AndroidRuntime(667): at android.widget.ListView.layoutChildren(ListView.java:1521)
09-30 08:58:38.762: ERROR/AndroidRuntime(667): at android.widget.AbsListView.onLayout(AbsListView.java:1113)
09-30 08:58:38.762: ERROR/AndroidRuntime(667): at android.view.View.layout(View.java:6830)


The error is a NullPoinerException although the ListSize is 1 (see the log)

Answer Source

It's because in your getView() method you are not inflating the convertView and it is returning you null.

Your getView should be something like this inflating a row.xml which contains a TextView.

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        TextView mTextView = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.row, parent, false);
            mTextView = (TextView) convertView.findViewById(R.id.txt_list);
            convertView.setTag(mTextView);
        }
        else{
            mTextView = (TextView) convertView.getTag();
        }
        mTextView.setText(arrList.get(position));
        return convertView;
    }