从数据库获取信息时SQLiteDatabase错误

时间:2014-08-31 17:47:07

标签: database sqlite

所以我创建了一个SQLiteDatabase,我创建了一个方法来从该数据库中获取信息,但每当我尝试访问它时,我的应用程序就会停止工作。这是我用来获取信息的方法,这是异常发生的地方:

public Product getProducts(Context ctx) {
    Product products = new Product();
    try {
        ourHelper = new DbHelper(ctx);
        db = ourHelper.getReadableDatabase();
        String projection[] = { DatabaseContract.KEY_ROWID,
                DatabaseContract.KEY_NAME, DatabaseContract.KEY_UM,
                DatabaseContract.KEY_SPECIFICATIONS };
        c = db.query(DatabaseContract.DATABASE_TABLE, projection, null,
                null, null, null, null);
        if (c.moveToFirst()) {
            do {
                products.setID(c.getString(c
                        .getColumnIndex(DatabaseContract.KEY_ROWID)), c
                        .getPosition());
                products.setName(c.getString(c
                        .getColumnIndex(DatabaseContract.KEY_NAME)), c
                        .getPosition());
                products.setUM(c.getString(c
                        .getColumnIndex(DatabaseContract.KEY_UM)), c
                        .getPosition());
                products.setOtherSpecs(
                        c.getString(c
                                .getColumnIndex(DatabaseContract.KEY_SPECIFICATIONS)),
                        c.getPosition());
            } while (c.moveToNext());
        }
        ourHelper.close();
        db.close();
        return products;
    } catch (Exception e) {
        System.out.println(Error.EXCEPTION_GETPRODUCTS);
    }
    return null;
}

这是产品类:

公共类产品{

private String ID[];
private String name[];
private String UM[];
private String otherSpecifications[];

public void setID(String setID, int index){
    ID[index] = setID;
}
public void setName(String setName, int index){
    name[index] = setName;
}
public void setUM(String setUM, int index){
    UM[index] = setUM;
}
public void setOtherSpecs(String setSpecs, int index){
    otherSpecifications[index] = setSpecs;
}
public String[] getID(){
    return ID;
}
public String[] getName(){
    return name;
}
public String[] getUM(){
    return UM;
}
public String[] getSpecs(){
    return otherSpecifications;
}

}

此外,如果你想要这是主要的Activity类:

public class MainActivity extends ActionBarActivity implements OnClickListener {

EditText ID, name, UM, specs;
Button save, load;
TextView data;
Database myDatabase;
Product products;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ID = (EditText)findViewById(R.id.etID);
    name = (EditText)findViewById(R.id.etName);
    UM = (EditText)findViewById(R.id.etUM);
    specs = (EditText)findViewById(R.id.etSpecs);
    save = (Button)findViewById(R.id.bSave);
    load = (Button)findViewById(R.id.bLoad);
    data = (TextView)findViewById(R.id.tvData);

    save.setOnClickListener(this);
    load.setOnClickListener(this);

    myDatabase = new Database();
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId()){
    case R.id.bSave:
        myDatabase.createProduct(this, ID.getText().toString(), name.getText().toString(), UM.getText().toString(), specs.getText().toString());
        break;
    case R.id.bLoad:
        products = myDatabase.getProducts(this);
        for(String x : products.getID())
            for(String xx : products.getName())
                for(String xxx : products.getUM())
                    for(String xxxx : products.getSpecs())
                        data.setText(x +"\t"+ xx +"\t"+ xxx +"\t"+ xxxx + "\n");
        break;
    }
}

这是日志:

08-31 18:17:39.811: E/AndroidRuntime(277): FATAL EXCEPTION: main
08-31 18:17:39.811: E/AndroidRuntime(277): java.lang.NullPointerException
08-31 18:17:39.811: E/AndroidRuntime(277):  at com.example.linicdecor.MainActivity.onClick(MainActivity.java:48)
08-31 18:17:39.811: E/AndroidRuntime(277):  at android.view.View.performClick(View.java:2408)
08-31 18:17:39.811: E/AndroidRuntime(277):  at android.view.View$PerformClick.run(View.java:8816)
08-31 18:17:39.811: E/AndroidRuntime(277):  at android.os.Handler.handleCallback(Handler.java:587)
08-31 18:17:39.811: E/AndroidRuntime(277):  at android.os.Handler.dispatchMessage(Handler.java:92)
08-31 18:17:39.811: E/AndroidRuntime(277):  at android.os.Looper.loop(Looper.java:123)
08-31 18:17:39.811: E/AndroidRuntime(277):  at android.app.ActivityThread.main(ActivityThread.java:4627)
08-31 18:17:39.811: E/AndroidRuntime(277):  at java.lang.reflect.Method.invokeNative(Native Method)
08-31 18:17:39.811: E/AndroidRuntime(277):  at java.lang.reflect.Method.invoke(Method.java:521)
08-31 18:17:39.811: E/AndroidRuntime(277):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-31 18:17:39.811: E/AndroidRuntime(277):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-31 18:17:39.811: E/AndroidRuntime(277):  at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案