我无法创建SQLite数据库。错误:java.lang.NullPointerException

时间:2013-10-25 18:54:03

标签: java android sqlite

以下是我的课程:

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
    }
}

现在最后当我运行我的应用程序时...这是以下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();
    }
}

}

现在,我被困在这一点上! 到目前为止,我只需保存一个值。我试图创建一个文件,而不是在数据库中保存一个值。但同样有一些ContextWrapper例外。

我目前有兴趣处理数据库。

如果您需要任何其他信息,请告诉我。

如果有任何人请解释这件事我真的很感激。我是android新手,很想知道这个问题。提前致谢!

1 个答案:

答案 0 :(得分:2)

StatsObjectId ob = new StatsObjectId();

您正在实现一个Activity类。你不被允许这样做。 (Android中应该有一些东西可以告诉你什么时候这样做)基本上,上下文没有被初始化,因为android需要这样做以便有一个功能性的Activity。

另外,Android(当它创建Activity时)使用适当的上下文调用onCreate方法。你没有(你也不能)因此你的db是空的。

在AbstractThreadedSyncAdapter中,您有一个getContext方法来获取正确的上下文。使用它来初始化数据库并在其中插入数据,而不是将其传递给Activity对象。