【问题标题】:Android SQLiteDatabase NullpointerExceptionAndroid SQLiteDatabase NullpointerException
【发布时间】:2011-09-25 18:32:16
【问题描述】:

好的完整代码:

DBOpenHelper:

public class DBOpenHelper extends SQLiteOpenHelper {

 private SQLiteDatabase mDatabase;
 private static final int DATABASE_VERSION = 1;
 private static String DATABASE_NAME = "RTDB";
 private static final String DB_TABLE_NAME1 = "playertable";
 private static final String DB_TABLE_NAME2 = "itemtable";
 private static final String DB_CREATE_TABLE_PT = 
         "CREATE TABLE IF NOT EXISTS" + DB_TABLE_NAME1 + " (" 
         + "ID INT(1) NOT NULL ,"
         + "Name VARCHAR(30) ,"
         + "HP INT(3) ,"
         + "Satisfaction INT(3) ,"
         + "Hygiene INT(1) , "
         + "IsAlive INT(1) "
         + " )"
         ;

 private static final String DB_CREATE_TABLE_IT = "CREATE TABLE IF NOT EXISTS"
            + DB_TABLE_NAME2 + " ("
            + "Money INT(3) ,"
            + "Gas INT(3) ,"
            + "Food INT(3) ,"
            + "Toiletries INT(3) ,"
            + "Spareparts INT(3) ,"
            + "Meds INT(3) ,"
            + "Tents INT(3) ,"
            + "Ration INT(1) ,"
            + "Trabbihp INT(3) ,"
            + "Trabbispeed INT(2) ,"
            + " )"
            ;


 public DBOpenHelper(Context context, String databaseName) {
        super(context,  databaseName, null, DATABASE_VERSION);
    }




@Override
public void onCreate(SQLiteDatabase db) {
    mDatabase = db;
    mDatabase = SQLiteDatabase.openOrCreateDatabase(DATABASE_NAME,null);
    mDatabase.execSQL(DB_CREATE_TABLE_PT);
    mDatabase.execSQL(DB_CREATE_TABLE_IT);
    DB.savePlayer(Resource.playerArray);
}


@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {

}

}

数据库:

public class DB {

static Context context;
private static DBOpenHelper dbHelper = new DBOpenHelper(context, "RTDB");
public static SQLiteDatabase db = dbHelper.getWritableDatabase();

private static ContentValues itemValues = new ContentValues();
private static ContentValues playerValues = new ContentValues();








// Speichern der Spieler in der Datenbank - playerarray muss �bergeben werden
public static void savePlayer(Player player[]){
    for(int i = 0; i<3; i++){
        playerValues.put("ID", i);
        playerValues.put("Name", player[i].getName());
        playerValues.put("HP", player[i].getHp());
        playerValues.put("Satisfaction", player[i].getsatisfaction());
        playerValues.put("Hygiene", player[i].isHygieneInt());
        playerValues.put("IsAlive", player[i].isAliveInt());

    }
    db.insert("playertable", null, playerValues);
}
// Speichern der Items
//TODO Position fehlt noch
public static void saveItems(){
    itemValues.put("Money", Resource.money);
    itemValues.put("Gas", Resource.gas);
    itemValues.put("Food", Resource.food);
    itemValues.put("Toiletries", Resource.toiletries);
    itemValues.put("Spareparts", Resource.spareparts);
    itemValues.put("Meds", Resource.meds);
    itemValues.put("Tents", Resource.tents);
    itemValues.put("Ration", Resource.ration);
    itemValues.put("Trabbihp", Resource.trabbihp);
    itemValues.put("Trabbispeed", Resource.trabbispeed);

    db.insert("itemtable",null,itemValues);
}

// Hier werden die Items aus der Datenbank abgefragt, der zurueckgelieferte Cursor vie cursorToIntArray() in einen Int Array umgewandelt und dessen Inhalt in die Ressource Klasse geschrieben
public void loadItems(){
    Cursor itemCursor = db.query("itemtable", null, null, null, null, null, null);
    int[] itemIntArray = cursorToInt(itemCursor, 9);

    Resource.money = itemIntArray[0];
    Resource.gas = itemIntArray[1];
    Resource.food = itemIntArray[2];
    Resource.toiletries = itemIntArray[3];
    Resource.meds = itemIntArray[4];
    Resource.tents = itemIntArray[5];
    Resource.ration = itemIntArray[6];
    Resource.trabbihp = itemIntArray[7];
    Resource.trabbispeed = itemIntArray[8];
}

//Name und Restliche Int-Werte der Playerobjekte werden separat aus der Datenbank geholt und gesetzt
public static void loadPlayer(){
    String[] namecolumn = {"Name"};
    String[] intcolumn = {"HP, Satisfaction, Hygiene, IsAlive"};
    String[] namesToString;

    for(int j=0;j<3;j++){
        Cursor playerCursorName = db.query("playertable", namecolumn, "ID="+j, null, null, null, null);
        namesToString = cursorToString(playerCursorName);
        Resource.playerArray[j].setName(namesToString[j]);
    }
    for(int i=0;i<3;i++){
        int[] restToInt;
        Cursor playerCursorInt = db.query("playertable", intcolumn, "ID="+i, null, null, null, null);
        restToInt = cursorToInt(playerCursorInt,4);
        Resource.playerArray[i].setHp(restToInt[i]);
        Resource.playerArray[i].setsatisfaction(restToInt[i]);
        Resource.playerArray[i].setHygieneInt(restToInt[i]);
        Resource.playerArray[i].setAliveInt(restToInt[i]);

    }
}
public void dropTables(){
    db.execSQL("DROP TABLE 'playertable';");
    db.execSQL("DROP TABLE 'itemtable';");
}

private static int[] cursorToInt(Cursor cursor, int n){
    int[] results = new int[n];
        for(int i=0 ;i<= n-1; i++){
            results[i] = cursor.getInt(i);
    }

    return results;
}
private static String[] cursorToString(Cursor cursor){
    String[] results = new String[4];
        for(int i=0 ;i<= 3; i++){
            results[i] = cursor.getString(i);
    }

    return results;
}

}

对于新读者:

public static SQLiteDatabase db = dbHelper.getWritableDatabase(); - 语句导致空指针异常

DBOpenHelper 是一个帮助类来创建数据库。它在 DB.java 中获取实例,我在其中创建了一些方法来操作数据库,例如 savePlayer 等

编辑:

在调试时,我在上面提到的那一行中发现了一些东西 dbHelper 对象还指向 mContext、mDatabase 等 - 正如您可能想象的那样 - null

atm 我正在尝试解决这个问题,但我找不到设置它们的方法

【问题讨论】:

    标签: android sqlite nullpointerexception


    【解决方案1】:

    我看到你没有初始化你的数据库:

    private static DBOpenHelper dbHelper = new DBOpenHelper(context);
    public static SQLiteDatabase db = dbHelper.getWritableDatabase();
    

    您需要在 DBOpenHelper 类中有一个构造函数,您可以在其中设置项目的数据库名称。您在 DB class 中设置数据库,但永远不要使用它:

    private static String filename = "RTDB.sql";
    

    这就是你得到NullPointerException的原因,因为没有你可以得到的数据库。

    编辑:你可以这样做:

    public DBOpenHelper(Context context, String databaseName) {
        super(context,  databaseName, null, DATABASE_VERSION);
    }
    

    这样,当你初始化你的数据库时,你可以设置你想要使用的数据库的名称。

    【讨论】:

    • 字符串“文件名”是我尝试过的另一种方法的痕迹。所以我必须在 DBOpenHelper 构造函数中添加一行,将数据库名称设置为我给构造函数的值?您必须知道,在超构造函数调用中的字符串“DATABASE_NAME”是我没有发布的字符串之一,顺便说一句,谢谢
    • 您确定您的数据库已创建。检查 DDMS 以查看它是否存在。
    • 这是我的问题的一部分,我不知道它是否已创建。 getWritableDatabase 调用应该创建它,不是吗?不知道在 DDMS 中在哪里查找数据库 - 但我猜它还不存在。
    • 签入 DDMS -> 数据 -> 你的包名 -> 数据,如果创建了数据库,你必须查看它。别忘了你必须在你的 onCreate 方法中创建你的数据库你的 DBOpenHelper 类。
    • 我认为数据库的实际创建会被处理。好的,现在我在 DBOpenHelper 的 onCreate 中添加了以下行:“SQLiteDatabase.openOrCreateDatabase("RTDB",null);"。猜想应该可以解决问题,现在我可以在 DDMS 中看到数据库。尽管如此,Nullpointer 问题仍然存在
    【解决方案2】:
    static Context context;
    private static DBOpenHelper dbHelper = new DBOpenHelper(context, "RTDB.sql");
    

    我猜是context == null

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多