我正在编写一个应用程序,用于检查数据库中的第一个值是否与字符串匹配。当我尝试连接到数据库时,我得到NullPointerException
。 getReadableDatabase()
和getWritableDatabase()
这是我的代码:
public class DataManager extends SQLiteOpenHelper
{
private final static String DB_TABLE = "Nums";
private final static String COL_KEY = "KEY";
private final static String COL_VALUE= "VALUE";
private static Context context;
private ContentValues initialValues;
private SQLiteDatabase db;
private static DataManager dm;
public static DataManager getInstance(Context _context)
{
if (dm==null)
{dm=new DataManager(_context);}
return dm;
}
private DataManager()
{
super(context, DB_TABLE, null, 1);
}
private DataManager(Context _context)
{
super(_context, DB_TABLE, null, 1);
context=_context;
initialValues = new ContentValues();
if (db==null)
{db=getWritableDatabase();}
}
@Override
public void onCreate(SQLiteDatabase db)
{
StringBuilder Query = new StringBuilder();
Query.append("CREATE TABLE IF NOT EXISTS ");
Query.append(DB_TABLE);
Query.append('(');
Query.append(COL_KEY);
Query.append(" TEXT PRIMARY KEY,");
Query.append(COL_VALUE);
Query.append(" TEXT);");
Log.i(Constants.TAG,"CREATE STRING: "+Query.toString());
db.execSQL(Query.toString());
if(tableEmpty())
{setDefault();}
}
/**
* Populate the database with numbers 1->MAXNUM giving each a value of 0.
*/
private void setDefault()
{
for (int i=1;i<=Constants.MAXNUM;i++)
{
setValue(String.valueOf(i),"0");
}
}
/**
* Method to get the values, ordered by frequency
* @return Comma seperated
*/
public String[] getAllValues()
{
Cursor c=null;
int counter=0;
String[] val = new String[Constants.MAXNUM];
try
{
c = getReadableDatabase().query(DB_TABLE, null,null, null, null, null, COL_VALUE);
c.moveToFirst();
//Ensure there is something in the database
if(c.getCount()>0)
{ // Make sure the cursor never goes over the edge
while(!c.isAfterLast())
{ // Append each value in order, seperated by a comma
val[counter++]=c.getString(1);
c.moveToNext();
}
}
}
catch(SQLiteException e){
Log.e(Constants.TAG,"getValue::SQLiteException::"+e.getMessage());
e.printStackTrace();
}
finally {
c.close(); // Tidy up
}
return val;
}
public String getValueByKey(String _key)
{
String val = "";
try
{
Log.i(Constants.TAG,"key is: "+_key);
Cursor c=getReadableDatabase().query(DB_TABLE,new String[]{COL_VALUE},COL_KEY + " LIKE ?",new String[]{_key},null,null,null);
c.moveToFirst();
if(c.getCount()>0)
{ val = c.getString(0); }
c.close();
}
catch(SQLiteException e)
{
Log.e(Constants.TAG,"SQLiteException::"+e.getMessage());
e.printStackTrace();
}
return val;
}
/**
* Method checks to see if there are any records in the database
* @return Boolean true if empty, false if not empty
*/
private boolean tableEmpty()
{
boolean result = false;
if(!(getValueByKey("1") == "0") )
{
result=true;
}
return result;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{}
}
我的数据库有2列。左列有键,右侧有值。我试图获取第一条记录的值并将其返回。错误消息是一个通用的NullPointerError,所以我不太了解错误,除了它与getReadableDatabase()
有关的事实。谁能看到我做错了什么?
感谢。
编辑:我已经添加了完整的代码。这是堆栈跟踪:09-23 15:16:27.450: ERROR/AndroidRuntime(11825): FATAL EXCEPTION: main
09-23 15:16:27.450: ERROR/AndroidRuntime(11825): java.lang.NullPointerException
09-23 15:16:27.450: ERROR/AndroidRuntime(11825): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825): at com.eoinzy.myApp.MyClass.openDatabase(MyClass.java:137)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825): at com.eoinzy.myApp.MyClass.getFrequency(MyClass.java:204)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825): at com.eoinzy.myApp.MyClass.tableEmpty(MyClass.java:252)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825): at com.eoinzy.myApp.MyClass.getAllValues(MyClass.java:169)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825): at com.eoinzy.myApp.MyClass.setupNumbers(MyClass.java:48)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825): at com.eoinzy.myApp.MyClass.<init>(MyClass.java:38)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825): at com.eoinzy.myApp.ButtonControl.onClick(ButtonControl.java:56)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825): at android.view.View.performClick(View.java:2501)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825): at android.view.View$PerformClick.run(View.java:9107)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825): at android.os.Handler.handleCallback(Handler.java:587)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825): at android.os.Handler.dispatchMessage(Handler.java:92)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825): at android.os.Looper.loop(Looper.java:130)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825): at android.app.ActivityThread.main(ActivityThread.java:3835)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825): at java.lang.reflect.Method.invokeNative(Native Method)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825): at java.lang.reflect.Method.invoke(Method.java:507)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
尝试这种编码方式,以便您可以轻松地调试错误。
同样Null Pointer
Null values
来自private Cursor cursor_value;
private DbHelper mDbHelper;
mDbHelper = new DbHelper(this);
mDbHelper.open();
try{
String cursor_link_value,cursor_time_value,cursor_name_value;
cursor_value = mDbHelper.fetch_message_values();
Log.v("cursor_value", ""+cursor_value.getCount());
startManagingCursor(cursor_value);
if (cursor_value.moveToFirst()) {
do {
value =cursor_value.getString(cursor_value.getColumnIndex(Column_Name));
} while (cursor_value.moveToNext());
}
}
catch(Exception e){
Log.v("Excepqqqqqqqqqqqqqqqqqqqq", ""+e);
}
。
DbHelper
对于DB:
在public Cursor fetch_message_values() {
// TODO Auto-generated method stub
Cursor c=mDb.rawQuery("Your Query",null);
Log.v("Cursor Count for Draft Table", ""+c.getCount());
return c;
}
类:
{{1}}
进一步参考检查Notepad Example for Database
的此链接