在android sqlite数据库中选择NullPointer异常

时间:2014-03-17 08:53:01

标签: android database sqlite

我已经编写了这段代码来选择count,last id,我在表格中作为参数发送的表的数据。 当我希望在将表名作为参数传递时选择NullPointer exceptionlastcounttotalcount时,我收到getdata。 我该怎么办?

这是我的数据库助手代码:

public String getaccounttable(int j) {
    // TODO Auto-generated method stub
    String col[]=new String[]{col_id,col_name,col_bankname,col_bal,col_accno,col_date};
    Cursor c=ourDB.query(dbtabelmain, col, col_id+"="+j, null, null, null, null);
    String result="";
    if(c!=null)
    {
    int iname=c.getColumnIndex(col_name);
    int i=c.getColumnIndex(col_accno);
    for(c.moveToFirst();!c.isAfterLast();c.moveToNext())
    {
        result=result+c.getString(iname)+c.getString(i);
    }

    }
    return result;
}

public void addtr(Long bal, String date, String account) {
    // TODO Auto-generated method stub
    ContentValues cvs=new ContentValues();
    cvs.put(col_bal_tb, bal);
    cvs.put(col_date_tb, date);
    ourDB.insert(account, null, cvs);

}

public int getidacccount(String account) {
    // TODO Auto-generated method stub
    String cl[]=new String[]{col_id_tb,col_bal_tb,col_date_tb};
    String acc=account;
    Cursor c=ourDB.query(acc, cl, null, null, null, null, null);
    if(c!=null)
    {
    int i=c.getCount();
    return i;
    }
    else
    {
        return 0;
    }

}

public int getacclastidcount(String account) {
    // TODO Auto-generated method stub
    String cl[]=new String[]{col_id_tb,col_bal_tb,col_date_tb};
    String acc=account;
    Cursor c=ourDB.query(acc, cl, null, null, null, null, null);
    String rs="";
    int i=c.getColumnIndex(col_id_tb);
    c.moveToLast();
    rs=c.getString(i);
    int j=Integer.parseInt(rs);
    return j;

}

public String getaccdata(int i, String account) {
    // TODO Auto-generated method stub
    String cl[]=new String[]{col_id_tb,col_bal_tb,col_date_tb};
    String acc=account;
    Cursor c=ourDB.query(acc, cl, col_id_tb+"="+i, null, null, null, null);
    String result="";
    if(c!=null)
    {
    int iname=c.getColumnIndex(col_bal_tb);
    int idate=c.getColumnIndex(col_date_tb);
    for(c.moveToFirst();!c.isAfterLast();c.moveToNext())
    {
        result=result+c.getString(iname)+"\n "+c.getString(idate);
    }

    }
    return result;

}       

这是我的类代码,它将account(tablename)作为参数传递给方法

Intent it=getIntent();


    final String pos=it.getStringExtra("ids");
    int x=Integer.parseInt(pos);
    PassBookAcc m =new PassBookAcc(ViewTransaction.this);
    m.open();
    final String account=m.getaccounttable(x);
    m.close();


    PassBookAcc info=new PassBookAcc(ViewTransaction.this);
    k=0;
    int size=info.getidacccount(account);
    String[] ex=new String[size];
    if(size==0)
    {
        setTitle("No Passbooks");
        info.close();
        ex[0]="NO PASSBOOKS";
    }
    else
    {
    i=info.getacclastidcount(account);

    String[] id=new String[i];

    for(j=0;j<i;j++)
    {
    id[j]=info.getaccdata(j+1,account);

    if(id[j]!="")
    {

        ex[k]=id[j];
        k++;
    }
    }
    info.close();
    }
    setListAdapter(new ArrayAdapter<String>(ViewTransaction.this,android.R.layout.simple_list_item_1,ex));

1 个答案:

答案 0 :(得分:0)

你的代码是正确的,我认为你应该使用

Cursor c=ourDB.query(dbtabelmain, col, col_id+"='"+j+"'", null, null, null, null);

而不是

Cursor c=ourDB.query(dbtabelmain, col, col_id+"="+j, null, null, null, null);