Android SQLiteDatabase NullpointerException

时间:2011-09-25 18:32:17

标签: android sqlite nullpointerexception

现在好了完整的代码:

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) {

}

}

DB:

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(); - 语句导致nullpointerexception

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

修改

调试时我发现上面提到的内容 dbHelper对象也指向mContext,mDatabase等 - 正如您可能想象的那样 - null

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

2 个答案:

答案 0 :(得分:0)

我认为你没有初始化你的数据库:

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);
}

以这种方式初始化数据库时,可以设置要使用的数据库的名称。

答案 1 :(得分:0)

static Context context;
private static DBOpenHelper dbHelper = new DBOpenHelper(context, "RTDB.sql");

我想context == null