IM在我EhActivity類剛開一個NullPointerException異常而打開數據庫NullPointerException異常openOrCreateDatabase的Android
public class EHActivity extends Activity {
ArrayList<String>ques= new ArrayList<String>();
ArrayList<String>ans=new ArrayList<String>();
SQLiteDatabase db2;
public ArrayList<String> getQues()
{
db2=openOrCreateDatabase("interview.db", MODE_PRIVATE, null);
db2.execSQL("create table eh (qid int primary key,ques varchar,ans char)");
db2.execSQL("insert into subject values(1,'What is an exception?','A1 '))");
db2.execSQL("insert into subject values(2,'What is error?','A2 '))");
Cursor c=db2.rawQuery("select * from eh",null);
while(c.moveToNext())
{
ques.add(c.getString(1));
}
return ques;
}
}
日誌貓的觀點是:
03-21 14:06:15.910:E/AndroidRuntime(955 ):java.lang.NullPointerException 03-21 14:06:15.910:E/AndroidRuntime(955):在android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203) 03-21 14:06:15.910:E/AndroidRuntime(955):在interview.android.EHActivity.getQues(EHActivity.java:19)
我懷疑你在調用onCreate()之前調用'getQues()'。從'EHActivity'的構造函數) - 你的活動上下文沒有初始化然後 – zapl 2012-03-21 09:08:36
@zapl我已經嘗試過使用onCreate()函數,但同樣的錯誤即將到來。 – shubh 2012-03-21 09:43:06