我正在寫一個應用程序,檢查數據庫中的第一個值是否與字符串匹配。當我嘗試連接到數據庫時,我得到一個NullPointerException
。這發生在兩個getReadableDatabase()
和getWritableDatabase()
Android - NullPointerException getReadableDatabase()
這裏是我的代碼:
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)
顯示烏爾'SqliteOpenHelper'類擴展的代碼 – Ronnie
我我只是擴展'SQLiteOpenHelper'。你在找什麼代碼? – eoinzy
請同時顯示堆棧跟蹤/異常日誌。 – Jack