0

I am using google's turn based multiplayer api. In the turn data class I am trying to access my sqlite database in order to put the information that needs to be passed to the next player in a byte array.

Here is the turn data class:

public class BattleTurnData  extends Activity
{

public static final String TAG = "EBTurn";

public String gameData = "";
public int turnCounter;
public static Context passedContext;

public BattleTurnData(Context context) 
{
    passedContext = context;
}

// This is the byte array we will write out to the TBMP API.
public byte[] persist() 
{
    //SharedPreferences prefs = getSharedPreferences("Buildings", MODE_PRIVATE);
    Database data = new Database(this);
    data.open();
    //int colonyHutOneID = prefs.getInt("NewColonyHutOne", 0);
    int colonyHutOneLevel = data.getColonyHutOneLevel();
    int colonyHutOneHealth = data.getColonyHutOneHealth();
    int colonyHutTwoLevel = data.getColonyHutTwoLevel();
    int colonyHutThreeLevel = data.getColonyHutThreeLevel();
    int colonyHutFourLevel = data.getColonyHutFourLevel();
    int colonyHutFiveLevel = data.getColonyHutFiveLevel();
    data.close();

    JSONObject retVal = new JSONObject();

    try {
        retVal.put("data", gameData);//colonyHutOneID + colonyHutOneLevel + colonyHutOneHealth + colonyHutTwoLevel);
        retVal.put("turnCounter", turnCounter);

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    String st = retVal.toString();

    Log.d(TAG, "==== PERSISTING\n" + st);

    return st.getBytes(Charset.forName("UTF-16"));
}

// Creates a new instance of BattleTurn.
static public BattleTurnData unpersist(byte[] byteArray) {

    if (byteArray == null) {
        Log.d(TAG, "Empty array---possible bug.");
        return new BattleTurnData(passedContext);
    }

    String st = null;
    try {
        st = new String(byteArray, "UTF-16");
    } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace();
        return null;
    }

    Log.d(TAG, "====UNPERSIST \n" + st);

    BattleTurnData retVal = new BattleTurnData(passedContext);

    try {
        JSONObject obj = new JSONObject(st);

        if (obj.has("data")) {
            retVal.gameData = obj.getString("data");
        }
        if (obj.has("turnCounter")) {
            retVal.turnCounter = obj.getInt("turnCounter");
        }

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return retVal;
}
}//end BattleTurn

Here is my logcat:

12-17 21:55:20.426: E/AndroidRuntime(1808): FATAL EXCEPTION: main
12-17 21:55:20.426: E/AndroidRuntime(1808): Process: com.project.llb, PID: 1808
12-17 21:55:20.426: E/AndroidRuntime(1808): java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
12-17 21:55:20.426: E/AndroidRuntime(1808):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:263)
12-17 21:55:20.426: E/AndroidRuntime(1808):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
12-17 21:55:20.426: E/AndroidRuntime(1808):     at  android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
12-17 21:55:20.426: E/AndroidRuntime(1808):     at com.twentytwentythree.sab.Database.open(Database.java:383)
12-17 21:55:20.426: E/AndroidRuntime(1808):     at com.twentytwentythree.sab.BattleTurnData.persist(BattleTurnData.java:33)
12-17 21:55:20.426: E/AndroidRuntime(1808):     at com.twentytwentythree.sab.BattleActivity.startMatch(BattleActivity.java:527)
12-17 21:55:20.426: E/AndroidRuntime(1808):     at com.twentytwentythree.sab.BattleActivity.processResult(BattleActivity.java:667)
12-17 21:55:20.426: E/AndroidRuntime(1808):     at com.twentytwentythree.sab.BattleActivity.access$0(BattleActivity.java:653)
12-17 21:55:20.426: E/AndroidRuntime(1808):     at com.twentytwentythree.sab.BattleActivity$1.onResult(BattleActivity.java:232)
12-17 21:55:20.426: E/AndroidRuntime(1808):     at com.twentytwentythree.sab.BattleActivity$1.onResult(BattleActivity.java:1)
12-17 21:55:20.426: E/AndroidRuntime(1808):     at com.google.android.gms.common.api.a$c.b(Unknown Source)
12-17 21:55:20.426: E/AndroidRuntime(1808):     at com.google.android.gms.common.api.a$c.handleMessage(Unknown Source)
12-17 21:55:20.426: E/AndroidRuntime(1808):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-17 21:55:20.426: E/AndroidRuntime(1808):     at android.os.Looper.loop(Looper.java:157)
12-17 21:55:20.426: E/AndroidRuntime(1808):     at android.app.ActivityThread.main(ActivityThread.java:5872)
12-17 21:55:20.426: E/AndroidRuntime(1808):     at java.lang.reflect.Method.invoke(Native Method)
12-17 21:55:20.426: E/AndroidRuntime(1808):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:852)
12-17 21:55:20.426: E/AndroidRuntime(1808):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:668)

And by the way, line 33 is the line that says data.open();

I don't understand why I am able to access the database in a different class like this but not in this class. I am new to all of this though so I still have much to learn. Any help is appreciated. Thanks guys.

sboehnke
  • 287
  • 1
  • 3
  • 13

1 Answers1

2

You've passed a null or otherwise invalid Context to your SQLiteOpenHelper constructor.


Looking at the stacktrace and your code, BattleTurnData extends Acitivity and explicit constructor BattleTurnData(Context context) looks suspicious. You cannot instantiate activities with new and the activities instantiated by the framework don't have constructors taking arguments. Therefore the this reference of that class is not a valid Context.

Looks like BattleTurnData should not be an activity but a regular class. Pass in a Context as an argument to methods that need it.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • I am not sure how to correct this. The classes in the api don't pass context between them. – sboehnke Dec 18 '14 at 20:18
  • Had a look at the stacktrace at first; now had a look at the code to give you further direction. – laalto Dec 18 '14 at 20:28
  • I'm sorry. I am new to all of this. So originally the BattleTurnData did not extend Activity. That was my way of trying to get context into it through specific calls. BattleTurnData is called by BattleTurn with is a class extending activity but I do not see it dealing with context anywhere. How do I give BattleTurnData context when this is so? – sboehnke Dec 18 '14 at 20:34
  • An `Activity` is-a `Context`. – laalto Dec 18 '14 at 20:35
  • Ah I get it now. Sorry for the dumb question. – sboehnke Dec 18 '14 at 20:44