Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6619641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:59:43+00:00 2026-05-25T20:59:43+00:00

Okay full code now: DBOpenHelper: public class DBOpenHelper extends SQLiteOpenHelper { private SQLiteDatabase mDatabase;

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:59:44+00:00Added an answer on May 25, 2026 at 8:59 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, so I'm running a small test webserver on my private network. I've got
Okay, so I'm making a table right now for Box Items. Now, a Box
I've been using Code::Blocks with MinGW on a WinXP box for a while now.
Okay, so I've got this TableLayout, and its full of data - with all
Okay so right now I'm working on a program that has a whole bunch
Okay, I'm trying to load a file in Java using this code: String file
Okay, so I'm trying to make a full text search in multiple columns, something
I have this code: $str = '(Test)'; $final = preg_replace('/\[translate=([a-z]{2})(.*)\]'.preg_quote($matches[3][$i]).'\[\/translate(.*)\]/',$str,$final,-1,$ct); It handles a situation
Okay, so I've had this code that used to work just fine. I recently
Okay, I'm trying to implement a templated class of an array-based queue called Queue.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.