Hye everyone,
I am trying to make a 2d-char array, fill it with random letters(chars) and print it. If I do it like this I get a ArrayIndexOutOfBoundsException: 0. I am a newbee.
I import my prefs(width, height) from static getters in the class preferences.
Can anyone help me please?

thanx.
stroper

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 = new char[height][width];


    public void copyPreferences() {
        width = preferences.getWidth();
        height = preferences.getHeight();
    }

    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() {
        copyPreferences()
        fieldInit();
        fieldPrint();
    }

Recommended Answers

All 2 Replies

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.

thx!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.