Android SQLiteDatabase NullpointerException

2019-08-16 14:13发布

问题:

Okay full code now:

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

}

For new readers:

The public static SQLiteDatabase db = dbHelper.getWritableDatabase(); - statement causes a nullpointerexception

DBOpenHelper is a helperclass to create the Database. It gets instances in DB.java where I created some methods to operate on the database like savePlayer etc

EDIT:

While debugging I found something in the line mentioned above The dbHelper object points also to mContext, mDatabase etc which are - as you might have imagined - null

atm I'm trying to resolve this, but I can't find a way to set them

回答1:

As I see you didn't initialize your database :

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

You need to have a constructor in your DBOpenHelper class where you can set the name of database for your project.You are setting your database in your DB class,but never using it :

private static String filename = "RTDB.sql";

That's why you are getting NullPointerException, because there is no database which you can get.

EDIT : You can do something like this :

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

in that way when you are initializing your database you can set the name of database that you want to use.



回答2:

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

i guess context == null.