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