NullPointerException:尝试从空对象引用上的字段StoreData.store_id读取

时间:2016-09-05 03:40:05

标签: java android nullpointerexception

请帮助我这里的问题在哪里以及如何解决它。所以我不会得到null对象。就我而言,在我尝试阅读其他帖子之前,我很抱歉,但让我有些困惑。

这是我的StoreHelper

public StoreData getStore(String storeid) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery("select * from " + TABLE_NAME + " where "
        + COL_STORE_ID + "=?", new String[]{storeid});
    StoreData data = null;
    System.out.println("This is the problem ");
    if (c.moveToFirst()) {
        data = new StoreData(
            c.getString(c.getColumnIndex(COL_STORE_ID)),
            c.getString(c.getColumnIndex(COL_ACCOUNT_ID)),
            c.getString(c.getColumnIndex(COL_KOTA_ID)),
            c.getString(c.getColumnIndex(COL_STORE_NAME)),
            c.getString(c.getColumnIndex(COL_STORE_MANAGER)),
            c.getString(c.getColumnIndex(COL_STORE_ADDRESS)),
            c.getString(c.getColumnIndex(COL_STORE_TELEPHONE)),
            c.getString(c.getColumnIndex(COL_STORE_GEO_LAT)),
            c.getString(c.getColumnIndex(COL_STORE_GEO_LONG)),
            c.getString(c.getColumnIndex(COL_STORE_LEADTIME)),
            c.getString(c.getColumnIndex(COL_STORE_MD)));
        db.close();
        c.close();
    }
    return data;
}

这是StoreData

public class StoreData {
    public int img;
    public String store_id;
    public String account_id;
    public String kota_id;
    public String store_name;
    public String store_manager;
    public String store_address;
    public String store_telephone;
    public String store_geo_lat;
    public String store_geo_long;
    public String store_leadtime;
    public String store_md;

    public StoreData(String storeid, String accountid, String kotaid, String storename, String storemanager,
                     String storeaddress, String storetelephone, String storegeolat, String storegeolong,
                     String storeleadtime, String storemd) {
        super();
        this.store_id = storeid;
        this.account_id = accountid;
        this.kota_id = kotaid;
        this.store_name = storename;
        this.store_manager = storemanager;
        this.store_address = storeaddress;
        this.store_telephone = storetelephone;
        this.store_geo_lat = storegeolat;
        this.store_geo_long = storegeolong;
        this.store_leadtime = storeleadtime;
        this.store_md = storemd;
    }
}

这就是错误点在Activity

中的位置
public void inCaseOffline() {

    StoreHelper sh = new StoreHelper(ctx);
    Log.i("ncdebug", "Store ID(incaseoffline): " + storeid);
    StoreData sd = sh.getStore(storeid);

    // temporary
    StoreInfo.storeid = sd.store_id;
    StoreInfo.storename = sd.store_name;
    StoreInfo.storeaddress = sd.store_address;
    StoreInfo.storetelephone = sd.store_telephone;
    StoreInfo.storemanager = sd.store_manager;

    Log.i("Data", sd.store_id + "<->" + sd.store_geo_lat + "," + sd.store_geo_long);

    TextView tvname = (TextView) findViewById(R.id.tvStoreInfoName);
    tvname.setText(sd.store_name);

    ProductsHelper helper = new ProductsHelper(ctx);
    StoreInfo.storeproducts = helper.getProducts();

    StoreInfo.storepromotions = new PromotionListData[0];

    DisplayCatHelper helperDisplayCat = new DisplayCatHelper(ctx);
    StoreInfo.storeDisplayCat = helperDisplayCat.getCategories();

    PromoCatHelper helperPromoCat = new PromoCatHelper(ctx);
    StoreInfo.storePromoCat = helperPromoCat.getCategories();

    CompetitorCatHelper helperCompetitorCat = new CompetitorCatHelper(ctx);
    StoreInfo.storeCompetitorCat = helperCompetitorCat.getCategories();

    add_overlay(StoreInfo.storename, new
            LatLng(Double.parseDouble(sd.store_geo_lat),
            Double.parseDouble(sd.store_geo_long)));
}

这是我的日志:

Process: com.apps.userinarts.mobilemerchpre, PID: 5308 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.apps.userinarts.mobilemerchpre/com.apps.userinarts.MobileMerchPre.StoresCheckinActivity}: java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.apps.userinarts.MobileMerchPre.data.StoreData.store_id' on a null object reference
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                  at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                  at android.os.Looper.loop(Looper.java:148)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                               Caused by: java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.apps.userinarts.MobileMerchPre.data.StoreData.store_id' on a null object reference
                                                                                  at com.apps.userinarts.MobileMerchPre.StoresCheckinActivity.inCaseOffline(StoresCheckinActivity.java:93)
                                                                                  at com.apps.userinarts.MobileMerchPre.StoresCheckinActivity.onCreate(StoresCheckinActivity.java:83)
                                                                                  at android.app.Activity.performCreate(Activity.java:6237)
                                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                  at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                  at android.os.Looper.loop(Looper.java:148) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 个答案:

答案 0 :(得分:0)

只是因为您的查询返回了null游标对象。

因此,我建议您在getStore函数中进行两次修改,以避免任何崩溃问题。

这是查找String对象的查询。所以查询应该是这样的。

Cursor c = db.rawQuery("select * from " + TABLE_NAME + " where "
    + COL_STORE_ID + "='" + new String[]{storeid} + "'", null);

仔细查看您提供的'之前和之后storeid作为查询的参数。

另一件事是,在对null

进行任何操作之前,您需要检查Cursor
// Check for null here and return if null
if(c == null) return null;

if (c.moveToFirst()) {
    //.. Your code
}

在这里添加支票

public void inCaseOffline() {

    StoreHelper sh = new StoreHelper(ctx);
    Log.i("ncdebug", "Store ID(incaseoffline): " + storeid);
    StoreData sd = sh.getStore(storeid);

    // Add the null checking
    if(sd == null) {
        Toast.makeText(YourActivity.this, "Could not fetch information", Toast.LENGTH_LONG).show();
        return;
    }

    // .. Your code
}

修改

您可以考虑将getStore功能修改为

public StoreData getStore(String storeid) {
    // ... Database operation. 

    // Initialize the StoreData here
    StoreData data = new StoreData();

    // ... Your code 
    return data;
}

要实现初始化,请在StoreData

中添加一个空构造函数
public class StoreData {
    public int img;
    public String store_id;
    public String account_id;
    public String kota_id;
    public String store_name;
    public String store_manager;
    public String store_address;
    public String store_telephone;
    public String store_geo_lat;
    public String store_geo_long;
    public String store_leadtime;
    public String store_md;

    public StoreData() {
        // Empty constructor
    }

    // ... The other constructer
}