Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom SimpleCursorAdapter, database query and NullPointerException
    text
    copied!<p>I'm trying to make ListView populated from database, and spice each row with fancy delete button. So I made list Activity and custom SimpleCursorAdapter.</p> <p>This is main ListView activity:</p> <pre><code> public class EditEntries extends ListActivity { Cursor cursor; DBAdapter db = new DBAdapter(this); private static String[] FROM = { _ID, SCORE_COLUMN, "date(time, 'localtime')", }; private static int[] TO = { R.id.rowid, R.id.title, R.id.time, }; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.entries_list); try { cursor = getEvents(); showEvents(cursor); } finally { db.close(); } } private void showEvents(Cursor cursor) { // Set up data binding SMSimpleCursorAdapter adapter = new SMSimpleCursorAdapter(this, R.layout.entries_list_item, cursor, FROM, TO); setListAdapter(adapter); } private Cursor getEvents() { db.open(); cursor = db.getAllScores(); startManagingCursor(cursor); return cursor; } void delRow() { // this is method for deleting row by _id db.open(); db.deleteScore(3); // here will be TAG with _id from adapter, db.close(); // for now I just use hardcoded _id } } </code></pre> <p>And this is custom SimpleCursorAdapter:</p> <pre><code> public class SMSimpleCursorAdapter extends SimpleCursorAdapter{ Cursor c; Context context; Activity activity; public SMSimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { super(context, layout, c, from, to); this.c = c; this.context=context; this.activity=(Activity) context; } EditEntries dbDel = new EditEntries(); //from previous code sample @Override public View getView(int position, View convertView, ViewGroup parent) { if(convertView == null) convertView = View.inflate(context, R.layout.entries_list_item, null); View row = convertView; c.moveToPosition(position); TextView score = (TextView) convertView.findViewById(R.id.title); TextView time = (TextView) convertView.findViewById(R.id.time); TextView id = (TextView) convertView.findViewById(R.id.rowid); id.setText(c.getString(0)); time.setText(c.getString(2)); score.setText(c.getString(1)); String daTag = c.getString(1); ImageButton delButton = (ImageButton) convertView.findViewById(R.id.delButton); delButton.setFocusable(true); delButton.setClickable(true); //delButton.setTag(daTag); delButton.setOnClickListener(new OnClickListener() { //Click listener fro delete button @Override public void onClick(View view) { dbDel.delRow(); //this is "delete row" method from previos code sample } }); return(row); } } </code></pre> <p>And all this stuff gives me NullPointerException when I click delete button. I'm new to android, and suppose that I just missed something obvious.</p> <p>LogCat:</p> <blockquote> <p>ERROR/AndroidRuntime(14027): FATAL EXCEPTION: main</p> <p>ERROR/AndroidRuntime(14027): java.lang.NullPointerException</p> <p>ERROR/AndroidRuntime(14027): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)</p> <p>ERROR/AndroidRuntime(14027): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)</p> <p>ERROR/AndroidRuntime(14027): at namespace.DBAdapter.open(DBAdapter.java:66)</p> <p>ERROR/AndroidRuntime(14027): at namespace.EditEntries.delRow(EditEntries.java:50)</p> <p>ERROR/AndroidRuntime(14027): at namespace.SMSimpleCursorAdapter$1.onClick(SMSimpleCursorAdapter.java:64)</p> <p>ERROR/AndroidRuntime(14027): at android.view.View.performClick(View.java:2414)</p> <p>ERROR/AndroidRuntime(14027): at android.view.View$PerformClick.run(View.java:8838)</p> <p>ERROR/AndroidRuntime(14027): at android.os.Handler.handleCallback(Handler.java:587)</p> <p>ERROR/AndroidRuntime(14027): at android.os.Handler.dispatchMessage(Handler.java:92)</p> <p>ERROR/AndroidRuntime(14027): at android.os.Looper.loop(Looper.java:123)</p> <p>ERROR/AndroidRuntime(14027): at android.app.ActivityThread.main(ActivityThread.java:4680)</p> <p>ERROR/AndroidRuntime(14027): at java.lang.reflect.Method.invokeNative(Native Method)</p> <p>ERROR/AndroidRuntime(14027): at java.lang.reflect.Method.invoke(Method.java:521)</p> <p>ERROR/AndroidRuntime(14027): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)</p> <p>ERROR/AndroidRuntime(14027): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)</p> <p>ERROR/AndroidRuntime(14027): at dalvik.system.NativeStart.main(Native Method)</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload