使用游标执行查询时出现IllegalArgumentException

时间:2014-01-04 09:36:54

标签: java android sqlite

我的查询是关于游标的。当我执行以下代码时,程序抛出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;

  }

logcat文件如下:

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)

主要活动文件是:

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;  


    }
  }

}

我还使用SQLdatabaseViewer查看数据库,以确保正确输入数据。 它显示了我输入的确切条目。 不知道为什么我仍然得到nullPointer异常。 请告诉我为什么会发生这种错误并解决相同问题。 我非常感谢你。

1 个答案:

答案 0 :(得分:0)

在这里:

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可能为null,无法绑定。绑定参数索引从1开始,因此索引2处的问题绑定引用第二个绑定arg。

同样在这里,你的光标包含两列

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

但你要求三个:

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

在尝试使用列索引-1时最终会导致另一个异常。