0

Here are following my classes:

StatsObjectId.java

public class StatsObjectId  extends Activity {
    DBClass db;
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    db = new DBClass(this);
    }
    public void addObjId(String objid){
    Log.e("objectid","This is the object id going to store: "+objid);


            db.addObjectId(objid);  //This is the line# 105


            if(getObjId()){
        Log.e("objectid","Successfully stored!");
    }else{
        Log.e("objectid","Error in storing object id!");
        }
    }
    public boolean getObjId(){
    boolean result;
    try{
    c = db.getObjectId();
    c.moveToFirst();
    String str = c.getString(c.getColumnIndex("objectid"));
    Log.e("objectid","Object id returned form DB: "+str);
    result = true;
    }catch(CursorIndexOutOfBoundsException e){
        Log.e("objectid","Cursor index out of bound");
        result = false;
        e.printStackTrace();
    }catch(Exception e){
        Log.e("objectid","Some Another Exception");
        result = false;
        e.printStackTrace();
    }
    return result;
}

ParseComServerAccessor.java

public class ParseComServerAccessor {
//I am skipping some irrelevant code
     public void putStats(String authtoken, String userId, Tas statsToAdd) throws Exception {
     //Again skip some code
     //Here I got some HttpResponse and I need to extract an object id and save it to database
     HttpResponse response = httpClient.execute(httpPost);
     String responseString = EntityUtils.toString(response.getEntity());
     JSONObject json = new JSONObject(responseString);
     Log.e("objectid","Now Object Id is: "+json.getString("objectId") );
     StatsObjectId ob = new StatsObjectId();


     ob.addObjId(json.getString("objectId")); // This is the line#156
     //skip some code
     }
}

TasSyncAdapter.java

public class TasSyncAdapter extends AbstractThreadedSyncAdapter {
    //skipped Constructor code
    public void onPerformSync(Account account, Bundle extras, String authority,
    ContentProviderClient provider, SyncResult syncResult) {
    //skipped some code
    ParseComServerAccessor parseComService = new ParseComServerAccessor();
    //skipped some code again


    parseComService.putStats(authToken, userObjectId, remoteTas); //This is the line# 134
    //skip some code
    }
}

Now finally when I run my app... this is the following Log Cat

Tag                    Text

objectid               This is the object id going to store: 9AFysqffz7
System.err             java.lang.NullPointerException
System.err             at com.myapp.ds_app.StatsObjectId.addObjId(StatsObjectId.java:105)
System.err             at com.myapp.ds_app.syncadapter.ParseComServerAccessor.putStats(ParseComServerAccessor.java:156)
System.err             at com.myapp.ds_app.syncadapter.TasSyncAdapter.onPerformSync(TasSyncAdapter.java:134)
System.err             at android.content.AbstractThreadedSyncAdapter$SyncThread.run(AbstractThreadedSyncAdapter.java:254)

DBClass.java

public class DBClass extends SQLiteOpenHelper { 
private static final String DATABASE_NAME="myapp.db";
public DBClass(Context cxt){
    super(cxt, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase mydatabase) {
    mydatabase.execSQL("CREATE TABLE IF NOT EXISTS temp (objectid STRING)");
}
public Cursor getObjectId(){
    Cursor cursor = getReadableDatabase().rawQuery("SELECT objectid FROM temp", null);
    return cursor;
}
public void addObjectId(String objid){
    try{
    ContentValues cv = new ContentValues(1);

    Log.e("objectid","In DBClass and object id: "+objid);
    cv.put("objectid", objid);
    Log.e("objectid","Content value contains: "+cv.toString());
    getWritableDatabase().insert("temp", "objectid", cv);

    }catch(NullPointerException e){
        e.printStackTrace();
    }
}

}

Now, I am stucked at this point! So far, I need to save just a single value. I tried to create a file instead of saving a value in database. But again there is some exception of ContextWrapper.

I am currently interested to deal with database.

Please let me know if you guys need any other information.

I would really appreciate if any one please explain this thing. I'm android newbie and would love to learn about this problem. Thanks in advance!

5
  • 3
    Lots of code. Please drill it down to what is relevant to your problem. Commented Oct 25, 2013 at 18:54
  • Just a shot, I would say db is null at line 105. Where is it initialized? Commented Oct 25, 2013 at 19:00
  • @Prateek Because, I want you guys to provide maximum things I can! Commented Oct 25, 2013 at 19:03
  • @dic19 what should I do then ? Where to initialized it? Commented Oct 25, 2013 at 19:04
  • @Yasir sometimes less is more. When you are finding a niddle in a haystack the smaller the haystack more easy is your task. Commented Oct 25, 2013 at 19:04

1 Answer 1

2
StatsObjectId ob = new StatsObjectId();

You are instanciating an Activity class. You are not allowed to do that. (There should really be something in Android to tell you when you do that) Basically, the context is not initialized, because android needs to do that in order to have a functional Activity.

Plus, Android (when it creates the Activity) calls the onCreate method with a proper context. You don't (and you can't, either), therefore your db is null.

In AbstractThreadedSyncAdapter, you have a getContext method to get a proper context. Use this to initialize your database and to insert data in it, rather than passing it to the Activity object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.