本文介绍了NullPointerException openOrCreateDatabase Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在打开数据库时在我的 EhActivity 类中得到 NullPointerException
i m gettin a NullPointerException in my EhActivity class while opening a database
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.NullPointerException03-21 14:06:15.910: E/AndroidRuntime(955): 在 android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)03-21 14:06:15.910: E/AndroidRuntime(955): at interview.android.EHActivity.getQues(EHActivity.java:19)
推荐答案
尽量不要发送 null..openOrCreateDatabase 有另一种方法用于此..
try not sending null..openOrCreateDatabase has another method for this..
db2=openOrCreateDatabase("interview.db", MODE_PRIVATE);
或
openDatabase("interview.db", MODE_PRIVATE, SQLiteDatabase.CREATE_IF_NECESSARY)
这篇关于NullPointerException openOrCreateDatabase Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!