我正在制作一个将食品杂货添加到列表的应用程序,我有一个包含所有属性的Product类,因此我试图根据其ID从数据库表中获取产品并返回它,但它保持告诉我,只要我调用函数,我的应用就会停止运行,这是我的代码
public Product GetProductById(String id)
{
SQLiteDatabase db=this.ourHelper.getReadableDatabase();
Product p=null;
Cursor cr;
cr = db.query(PRODUCTS_TABLE, null, PRODUCT_ID+"="+id, null, null, null, null);
cr.moveToFirst();
if(!cr.isAfterLast()) {
p=new Product();
p.setId(cr.getInt(0));
p.setName(cr.getString(1));
p.setQuantity(cr.getInt(2));
p.setPrice(cr.getDouble(3));
p.setAllergens(cr.getString(4));
p.setPurchased(cr.getInt(5));
p.setImage(cr.getBlob(6));
p.setPriority(cr.getInt(7));
}
return p;
}
此代码不起作用,我不知道错误在哪里。 这是我的数据库列(名称和类型...):
public static final String DB_NAME="db_groceries";
public static final int DB_VER=1;
public static final String PRODUCT_NAME = "name";
public static final String PRODUCT_QUANTITY = "quantity";
public static final String PRODUCT_PRICE = "price";
public static final String PRODUCT_ALLERGENS = "allergens";
public static final String PRODUCT_PRIORITY = "priority";
public static final String PRODUCT_ID = "_id";
public static final String PRODUCT_IMAGE = "image";
public static final String PRODUCTS_TABLE = "groceries";
public static final String PRODUCTS_PURCHASED = "purchased";
public static final String SCRIPT_CREATE_PRODUCTS_TABLE = "create table "
+ PRODUCTS_TABLE + "(" + PRODUCT_ID +
" integer primary key autoincrement," +
PRODUCT_NAME + " text not null," +
PRODUCT_QUANTITY + " integer not null," +
PRODUCT_PRICE + " real not null," +
PRODUCT_ALLERGENS + " text not null, " +
PRODUCTS_PURCHASED + " integer not null, " +
PRODUCT_IMAGE + " blob," +
PRODUCT_PRIORITY + " integer not null)";
你能弄清楚我的错误在哪里吗?
答案 0 :(得分:0)
我相信您的问题是,在调用GetProductById方法之前,您尚未实例化 ourHelper 。
如果还没有,那么您将获得类似于:-
的空指针异常11-01 19:54:35.925 1420-1420/axtest.axtest E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{axtest.axtest/axtest.axtest.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at axtest.axtest.MainActivity.GetProductById(MainActivity.java:134)
at axtest.axtest.MainActivity.onCreate(MainActivity.java:35)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
要解决此问题,您需要按照
的方式实例化ourHelper**`ourHelper = new DatabaseHelper(this);`**
请注意Databasehelper可能有所不同(您尚未说出类名是什么),并且签名也可能有所不同(尽管签名通常如上所述,但它取决于Database helper中的代码)
否则,您的代码可以正常工作。但是,也可能是您更改了表的结构但未删除数据库。要排除这种情况,请删除该应用程序的数据,然后重新运行该应用程序。