Android SQLiteDatabase NullpointerException [英] Android SQLiteDatabase NullpointerException

查看:40
本文介绍了Android SQLiteDatabase NullpointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,现在完整代码:

DBOpenHelper:

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(); - 语句导致空指针异常

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

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

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

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

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 我正在尝试解决这个问题,但我找不到设置它们的方法

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

推荐答案

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

As I see you didn't initialize your database :

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

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

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

这就是您得到 NullPointerException 的原因,因为没有您可以获得的数据库.

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

你可以这样做:

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.

这篇关于Android SQLiteDatabase NullpointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆