IllegalArgumentException while performing query with cursors

My Query is regarding cursors.When I execute the following code the program throws NUllPointerException.

public class MySQLiteHelper extends SQLiteOpenHelper {

  public MySQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    // TODO Auto-generated constructor stub
  }

  public static final String DATABASE_NAME = "Exp.db";
  public static final int DATABASE_VERSION = 1;
  private static final String KEY_ID = "ROW_ID";
  private static final String KEY_DESCRIP = "ITEM_DESCRIPTION";
  private static final String KEY_AMOUNT = "ITEM_AMOUNT";
  private static final String KEY_TABLE_NAME = "ITEMS";

  @Override
  public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL("create table ITEMS(ROW_ID integer primary key autoincrement,ITEM_DESCRIPTION text,ITEM_AMOUNT text);");
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
    // TODO Auto-generated method stub

    db.execSQL("DROP TABLE IF EXISTS ITEMS");
    this.onCreate(db);
  }

  public void createEntry(ExpenseObject exp) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(KEY_AMOUNT, exp.Amt);
    cv.put(KEY_DESCRIP, exp.Desc);
    db.insert(KEY_TABLE_NAME, null, cv);
    db.close();
  }

  public String getAllEntry() {
    SQLiteDatabase db = this.getReadableDatabase();
    String[] columns=new String[]{KEY_ID,KEY_DESCRIP,KEY_AMOUNT};
    Cursor cx=db.query(KEY_TABLE_NAME, columns, null, null, null, null, null);
        String result=" ";
        int rowid=cx.getColumnIndex(KEY_ID);
        int amt=cx.getColumnIndex(KEY_AMOUNT);
        int desc=cx.getColumnIndex(KEY_DESCRIP);
        for(cx.moveToFirst();!cx.isAfterLast();cx.moveToNext())
        {
          result= result + " " + cx.getString(rowid) + " " +cx.getString(desc) + "  " + cx.getString(amt) + " \n " ;
        }
        cx.close();

       return result;

  }

The logcat file is as:

D/dalvikvm(4318): GC_FOR_ALLOC freed 77K, 6% free 2943K/3120K, paused 57ms, total 67ms
D/AndroidRuntime(4318): Shutting down VM
W/dalvikvm(4318): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
E/AndroidRuntime(4318): FATAL EXCEPTION: main
E/AndroidRuntime(4318): java.lang.NullPointerException
E/AndroidRuntime(4318): at com.example.expensesdatabase.MainActivity.onClick(MainActivity.java:61)
E/AndroidRuntime(4318):at android.view.View.performClick(View.java:4240)
E/AndroidRuntime(4318):at android.view.View$PerformClick.run(View.java:17721)
E/AndroidRuntime(4318):at android.os.Handler.handleCallback(Handler.java:730)
E/AndroidRuntime(4318):at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(4318):at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(4318):at android.app.ActivityThread.main(ActivityThread.java:5103)
E/AndroidRuntime(4318):at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(4318):at java.lang.reflect.Method.invoke(Method.java:525)
E/AndroidRuntime(4318):at com.an>droid.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:737)
E/AndroidRuntime(4318):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime(4318):at dalvik.system.NativeStart.main(Native Method)

The main activity file is:

public class MainActivity extends Activity implements View.OnClickListener {
  EditText desc,amt,r;
  Button enter,bview;
  TextView s1,i1,a1;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    desc=(EditText) findViewById(R.id.eDesc);
    amt=(EditText) findViewById(R.id.eAmt);
    enter=(Button) findViewById(R.id.bEnter);
    bview=(Button) findViewById(R.id.bview);
    s1=(TextView) findViewById(R.id.tvid1);
    a1=(TextView) findViewById(R.id.tvamt1);
    i1=(TextView) findViewById(R.id.tvitem1);
    r=(EditText) findViewById(R.id.editText1);
    enter.setOnClickListener(this);
    bview.setOnClickListener(this);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }

  @Override
  public void onClick(View v) {
    ExpenseObject e ;
    MySQLiteHelper h=new MySQLiteHelper(this);
    switch(v.getId()){
    case R.id.bEnter:
      String d=desc.getText().toString();
      String ab=amt.getText().toString();

      e =new ExpenseObject(d,ab);   
      h.createEntry(e);
      Dialog x=new Dialog(this);
      x.setTitle("Success");
      TextView tv= new TextView(this);
      tv.setText("Entry created");
      x.setContentView(tv);
      x.setCancelable(true);
      x.show();
      break;

    case R.id.bview:
      setContentView(R.layout.view_database);
          String result=h.getAllEntry();
          r.setText(result);

    break;  


    }
  }

}

I also Viewed the Database using SQLdatabaseViewer to ensure that data was properly entered into it. And it showed the exact entries which I had entered. Don't know why still I am getting nullPointer exception. Please tell me why this error is occurring and solution to the same. I would be highly thankful to you.

Answers


In here:

ContentValues cv = new ContentValues();
cv.put(KEY_AMOUNT, exp.Amt);
cv.put(KEY_DESCRIP, exp.Desc);
db.insert(KEY_TABLE_NAME, null, cv);

exp.Desc is possibly null and cannot be bound. Bind arguments indexing starts at 1 so a problem binding at index 2 refers to the second bind arg.

Also in here, your cursor contains two columns

private static final String[] columns = { KEY_DESCRIP, KEY_AMOUNT };
Cursor cx = db.query(KEY_TABLE_NAME, columns, null, null, null, null, null);

but you are requesting for three:

int rowid=cx.getColumnIndex(KEY_ID);
int amt=cx.getColumnIndex(KEY_AMOUNT);
int desc=cx.getColumnIndex(KEY_DESCRIP);

which will eventually cause another exception when trying to use column index -1.


Need Your Help

Begin...End blocks in SQL Server: Is this code right?

sql-server syntax

I just want to verify that I'm not missing something here... so bear with me.

get table name of resultset Mysql

php mysql stored-procedures pdo multiple-resultsets

I am using PHP PDO. My stored procedure returns two result sets. Is it possible to name the result sets? In case they are SELECT statements on two tables like,