When you declare this:
public char[][] bogglefield = new char[height][width];
The height, width variables are undefined, so they are zero, so the bogglefield array has 0x0 size.
At the method: copyPreferences you give values to the height, width, but the array bogglefield has already been created with 0 size.
So I would suggest this:
public char[][] bogglefield = null;
....
public void copyPreferences() {
width = preferences.getWidth();
height = preferences.getHeight();
bogglefield = new char[height][width];
}
Or better, just to make sure that copyPreferences is always called first, because someone might forget to call that method and the array would be null:
class Playgame {
private final List<Integer> lengte = new ArrayList<Integer>();
private int width;
private int height;
private Random random = new Random();
private static char[] alfabet = { 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z' };
public char[][] bogglefield = null;
// CONSTRUCTOR. Do the initializations here
public Playgame() {
copyPreferences();
}
// ---->
public void copyPreferences() {
width = preferences.getWidth();
height = preferences.getHeight();
bogglefield = new char[height][width];
}
public void fieldInit(){
for(int i = 0; i < bogglefield.length;i++){
for(int j = 0; j < bogglefield.length; j++){
bogglefield[i][j] = alfabet[random.nextInt(26)];
}
}
}
public void fieldPrint(){
for (int i =0; i < height; i++) {
for (int j = 0; j < width; j++) {
System.out.print(" " + bogglefield[i][j]);
}
System.out.println("");
}
}
void start() {
// REMOVE FROM HERE
//copyPreferences();
fieldInit();
fieldPrint();
}
Or you can put the start method in the constructor as well.
Also it would be a good thing to have your class names to start with capital. It is common practice. If you have time you may rename the preferences class to
Preferences.
javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448