Pre Exam Test Practice (SSC | Banking | IAS | GATE | State Level | CLAT | Railways | Insurance | Teaching Exams | NDA | Defence | IES |State Exam )

Organizational Research By

Surprising Reserch Topic

app crashes on startup due to java lang illegalargumentexception column id d using -'android,sqlite,crash,cursor'

app crashes on startup due to java lang illegalargumentexception column id d  using -'android,sqlite,crash,cursor'

Whenever I start my app, I get a java.lang.IllegalArgumentException: column '_id' does not exist error in my LogCat. I have created the column '_id', but it still throws this. Here is my main .java:

package com.gantt.shoppinglist;

import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class ShoppingList extends ListActivity {

    private DataHelper DataHelper;
    /** Called when the activity is first created. */
    protected void onCreate(Bundle savedInstanceState) {
        DataHelper = new DataHelper(this);

        Cursor c = (Cursor) DataHelper.selectAll();
        long id = c.getLong(c.getColumnIndex("_id"));

        ListView lv = (ListView) findViewById(;

        String[] from = new String[] { com.gantt.shoppinglist.DataHelper.getDatabaseName() };
        int[] to = new int[] { };

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_1, c, from, to);       

        Button button1main = (Button) findViewById(;
        button1main.setOnClickListener(new OnClickListener()  {
            public void onClick(View v)  {
            final Dialog additem = new Dialog(ShoppingList.this);
            final EditText et = (EditText)additem.findViewById(;
            additem.setTitle("Type your item");
            et.setHint("Type the name of an item...");

            Button button = (Button) additem.findViewById(;
            button.setOnClickListener(new OnClickListener()  {
                public void onClick(View v)  {

            Button ok = (Button) additem.findViewById(;
            ok.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    final String text = et.getText().toString();

Here is my DataHelper class:

package com.gantt.shoppinglist;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

public class DataHelper {

       private static final String DATABASE_NAME = "items.db";
       private static final int DATABASE_VERSION = 1;
       private static final String TABLE_NAME = "table1";
       public static final String KEY_ROWID = "_id";

       private Context context;
       private SQLiteDatabase db;

       private SQLiteStatement insertStmt;
       private static final String INSERT = "insert into "
          + TABLE_NAME + "(name) values (?)";

       public DataHelper(Context context) {
          this.context = context;
          OpenHelper openHelper = new OpenHelper(this.context);
          this.db = openHelper.getWritableDatabase();
          this.insertStmt = this.db.compileStatement(INSERT);

       public long insert(String name) {
          this.insertStmt.bindString(1, name);
          return this.insertStmt.executeInsert();

       public void deleteAll() {
          this.db.delete(TABLE_NAME, null, null);

       public Cursor selectAll() {
          List<String> list = new ArrayList<String>();
          Cursor cursor = this.db.query(TABLE_NAME, new String[] { "name" },
            null, null, null, null, "name desc");
          if (cursor.moveToFirst()) {
             do {
             } while (cursor.moveToNext());
          if (cursor != null && !cursor.isClosed()) {
          return cursor;

       public static String getDatabaseName() {
        return DATABASE_NAME;

    private static class OpenHelper extends SQLiteOpenHelper {

          OpenHelper(Context context) {
             super(context, getDatabaseName(), null, DATABASE_VERSION);

          public void onCreate(SQLiteDatabase db) {
              db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, name TEXT");


          public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
             Log.w("Example", "Upgrading database, this will drop tables and recreate.");
             db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);


asked Sep 7, 2015 by rajesh
0 votes

Related Hot Questions

Government Jobs Opening