0

Please help me where is the problem here and how to fix it. So I wont get null object. In my case, I'm sorry before I try to read other post too but make me little bit confused.

Here is my 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;
}

This is the 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;
    }
}

And this is where the error point is in the 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)));
}

This is my log :

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)
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
L Wira Satria M
  • 67
  • 2
  • 13

1 Answers1

0

Simply because your query is returning a null cursor object.

So I would suggest two modification in your getStore function to avoid any crash issue.

This is a query to find a String object. So the query should look like this.

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

Look closely at the ' before and after the storeid you've provided as a parameter to the query.

And the other thing is you need to check for null value before you do any operation on your Cursor

// Check for null here and return if null
if(c == null) return null;

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

Add a check here too

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
}

Edit

You might consider having the getStore function to be modified like this

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

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

    // ... Your code 
    return data;
}

To achieve the initialization, add an empty constructor too in your StoreData class

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
}
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • Still same dude,, :( i dont know where is the exactly can make this error,, before in eclipse no error, but thanks for respond ,, im glad if you can help more about this :) – L Wira Satria M Sep 05 '16 at 04:30
  • Please see the edited answer. – Reaz Murshed Sep 05 '16 at 05:15
  • Thank you so much Reaz, it's show "Could not feth information" like the Toast, according to you where is the problem ? on my database or database code,, the apps load map base on location that save on database – L Wira Satria M Sep 05 '16 at 06:39
  • It might be the problem of saving the data properly in your database. Did you check `select * from TABLE_NAME` ? – Reaz Murshed Sep 05 '16 at 07:51
  • can i check it from system.out.println in that code ? – L Wira Satria M Sep 05 '16 at 09:40
  • You need to debug the application code. Put a breakpoint in the System.println line so that you can see what has been loaded in the cursor. – Reaz Murshed Sep 05 '16 at 09:41
  • no error in the database,, i think the problem at the line code here "I Set StoreData data = null" then after close bracket i write "return data;" and automatically return the data=null, my question how to return this object so not null occur. please help me – L Wira Satria M Sep 07 '16 at 03:28
  • Please see the edited answer and upvote if that helps – Reaz Murshed Sep 07 '16 at 05:10