0

Basically, I'm making a reversi app for Android, and I am currently at the point of setting up the game board. The way I am intending to do this is to create a 2D array of the position class. Each position is represented by an imageview that is used as a button which represents a board position. Here is a snippet of the code that contains the error:

setupBoard(board);
....
public void setupBoard(Position board[][]) {
    for (int y = 0; y < 8; y++) {
        for (int x = 0; x < 8; x++) {
            Log.d("Error","Error a");
            board[x][y] = new Position(getApplicationContext());
            Log.d("Error","Error b");
            board[x][y].isPositionEmpty = true;

        }
    }
}

In the position class:

public class Position {

Context myContext;
public Position(Context context) {
    Log.d("Error","Error c");
    myContext = context;
    // TODO Auto-generated constructor stub

}


public boolean isPositionEmpty = true;
public int positionID;


public  ImageView button = new ImageView(myContext);

}

The program gets to error a, but no further. The error in LogCat is: java.lang.NullPointerException

Any help will be much appreciated. Thanks in advance!

Exception: 02-20 16:52:20.820: ERROR/AndroidRuntime(365): FATAL EXCEPTION: main 02-20 16:52:20.820: ERROR/AndroidRuntime(365): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{trinity.hazard.reversi/trinity.hazard.reversi.SinglePlayerActivity}: java.lang.NullPointerException 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at android.os.Handler.dispatchMessage(Handler.java:99) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at android.os.Looper.loop(Looper.java:123) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at android.app.ActivityThread.main(ActivityThread.java:4627) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at java.lang.reflect.Method.invokeNative(Native Method) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at java.lang.reflect.Method.invoke(Method.java:521) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at dalvik.system.NativeStart.main(Native Method) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): Caused by: java.lang.NullPointerException 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at trinity.hazard.reversi.SinglePlayerActivity.(SinglePlayerActivity.java:24) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at java.lang.Class.newInstanceImpl(Native Method) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at java.lang.Class.newInstance(Class.java:1429) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): ... 11 more

0

1 Answer 1

2

In your Position class, try moving the line where you create the ImageView into the constructor, but AFTER initializing myContext.

Sign up to request clarification or add additional context in comments.

11 Comments

Oh duh, you're right. This is the problem. Good catch! However, I would suggest removing the reference to the Context from Position and just initialize the ImageView with it. The fewer the references to a Context, the better.
m1ntf4n: It wont let me, the singlePlayerActivity isnt able to see the buttons. thanks for the help anyway
Glad I could help. Always be aware that any initialization you do outside the constructor will actually be copied to the beginning of each constructor (but after a super()call, if I'm not mistaken).
dmon: Ive tried this before, but I couldnt find a valid context to use when creating the imageView
M1ntf4n: here are the first few lines of code: ' public void onCreate(Bundle savedInstantState){ super.onCreate(savedInstantState); setContentView(R.layout.singleplayer); Player humanPlayer = new Player(); final Position[][] board = new Position[8][8]; Log.d("random Int",String.valueOf(whoPlaysFirst)); playerName=getPlayerName(humanPlayer); Piece piece = new Piece(); if(whoPlaysFirst == 1){ playerTurn = true; } else if(whoPlaysFirst == 0){ playerTurn = false; } setupBoard(board);'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.