-4

Me and my research partner at school have trouble with our assignment about Conway's Game Of Life.

Our program is malfunctioning and looking at the code I see nothing wrong with our code and when looking at the error message and even the debug function I still cannot find out why it's malfunctioning.

The error message is as follows:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 36 at nl.ru.ai.exercise6.tryout.nextGeneration(tryout.java:84) at nl.ru.ai.exercise6.tryout.evolve(tryout.java:138) at nl.ru.ai.exercise6.tryout.main(tryout.java:27)

When using the debug function it says that there is a problem in line 84, nextUniverse[i][j] = Cell.DEAD; Can somebody maybe please look at our code and specify why our code doesn't work at that particular point. I know our code is very long, but we're still

public static void main(String[] args) throws IOException {
    System.out.print("Enter input file:");
    Scanner scanner = new Scanner(System.in);
    String fileName = scanner.nextLine();
    System.out.print("For how many generations do you want your program to run?");
    for (int row = 0; row < 40; row++) {
        for (int col = 1; col < 60; col++) {
    int numberEvolve = scanner.nextInt();
    Cell[][] universe = readUniverseFile(fileName);
    showUniverse(universe);
    evolve(universe, numberEvolve, 2, fileName, row, col);}}
}

static Cell[][] readUniverseFile(String fileName) throws IOException {
    Cell[][] universe = new Cell[40][60];
    try {
        BufferedReader in = new BufferedReader(new FileReader(fileName));

        try {
            for (int i = 0; i <= 39; i++) {
                String line = in.readLine();
                if (line == null) {
                    throw new IllegalArgumentException("Input file does not contain 40 lines");
                }
                if (line.length() != 60) {
                    throw new IllegalArgumentException(
                            "Input file contains a line which doesn't contain 60 characters");
                }
                for (int j = 0; j < 60; j++) {
                    if (line.charAt(j) == '*') {
                        universe[i][j] = Cell.LIVE;
                    } else if (line.charAt(j) == '.') {
                        universe[i][j] = Cell.DEAD;
                    } else if (line.charAt(j) != '.') {
                        throw new IllegalArgumentException("Input file contains invalid character");
                    } else if (line.charAt(j) != '*') {
                        throw new IllegalArgumentException("Input file contains invalid character");
                    }

                }
            }
            in.close();
        } catch (FileNotFoundException e) {
            throw new IllegalArgumentException("Input file could not be found");
        }

    } catch (FileNotFoundException e) {
        throw new IllegalArgumentException("Input file could not be found");
    }
    return universe;
}

private static void showUniverse(Cell[][] universe) {
    for (int row = 0; row <= 39; row++) {
        for (int col = 0; col <= 59; col++) {
            updateScreen(row, col, universe[row][col]);
        }
    }
}

private static Cell[][] nextGeneration(Cell[][] universe, String fileName, int row, int col) throws IOException {   
            Cell[][] nextUniverse = new Cell[row][col];
            for (int i = 1; i <= 38; i++) {
                for (int j = 1; j <= 58; j++) {
                    int livingNeighbours = numberLivingNeighbours(universe);
                    if (universe[i][j] == Cell.LIVE) {
                        if (livingNeighbours < 2 || livingNeighbours > 3) {
                            nextUniverse[i][j] = Cell.DEAD;
                        } else {
                            nextUniverse[i][j] = Cell.LIVE;
                        }
                    } else {
                        if (livingNeighbours == 3) {
                            nextUniverse[i][j] = Cell.LIVE;
                        }
                    }

                }
            }
    return nextUniverse;
}

private static int numberLivingNeighbours(Cell[][] universe) {
    int numberLivingNeighbours = 0;
    for (int row = 1; row < 39; row++) {
        for (int col = 1; col < 59; col++) {
            if (universe[row + 1][col] == Cell.LIVE) {
                numberLivingNeighbours++;
            }
            if (universe[row + 1][col - 1] == Cell.LIVE) {
                numberLivingNeighbours++;
            }
            if (universe[row + 1][col + 1] == Cell.LIVE) {
                numberLivingNeighbours++;
            }
            if (universe[row][col - 1] == Cell.LIVE) {
                numberLivingNeighbours++;
            }
            if (universe[row][col + 1] == Cell.LIVE) {
                numberLivingNeighbours++;
            }
            if (universe[row - 1][col] == Cell.LIVE) {
                numberLivingNeighbours++;
            }
            if (universe[row - 1][col - 1] == Cell.LIVE) {
                numberLivingNeighbours++;
            }
            if (universe[row - 1][col + 1] == Cell.LIVE) {
                numberLivingNeighbours++;
            } else {
                numberLivingNeighbours--;
            }
        }
    }
    return numberLivingNeighbours;
}

private static void evolve(Cell[][] universe, int numberEvolve, int generationTime, String fileName, int row, int col) throws IOException {
    for (int i = 0; i < numberEvolve; i++) {
        showUniverse(universe);
        sleep(generationTime);
        universe = nextGeneration(universe, fileName, row , col);
    }
}
sbglasius
  • 3,104
  • 20
  • 28
Nessa
  • 1
  • 1

2 Answers2

0
for (int row = 0; row < 40; row++) {
    for (int col = 1; col < 60; col++) {
      int numberEvolve = scanner.nextInt();
      Cell[][] universe = readUniverseFile(fileName);
      showUniverse(universe);
      evolve(universe, numberEvolve, 2, fileName, row, col);
    }
}

Here you call evolve() the first time with row = 0 and col = 1 These values are passed to nextGeneration() there:

universe = nextGeneration(universe, fileName, row , col);

There you call Cell[][] nextUniverse = new Cell[row][col]; (Remember, it's 0 and 1)

Then you start an iteration with fixed values i and j:

for (int i = 1; i <= 38; i++) {
  for (int j = 1; j <= 58; j++) {

And as soon as you call nextUniverse[i][j] = Cell.DEAD; with values i/j greater than 0/1 you get an ArrayOutOfBoundsException, cause your array size depends on the row/col input, but your iteration with i and j goes up to 38/58.

Happy fixing :)

Zyndoras
  • 33
  • 1
  • 10
0

On the first iteration of your for loops in the main method, row is 0 and col is 1

22 for (int row = 0; row < 40; row++) {
23     for (int col = 1; col < 60; col++) {
24         int numberEvolve = scanner.nextInt();
25         Cell[][] universe = readUniverseFile(fileName);
26         showUniverse(universe);
           // On the first iteration, row is 0 and col is 1 when calling evolve() method
27         evolve(universe, numberEvolve, 2, fileName, row, col);}}

After which, nextGeneration() is called with the variables row = 0 and col = 1

134 private static void evolve(Cell[][] universe, int numberEvolve, int generationTime, String fileName, int row, int col) throws IOException {
135     for (int i = 0; i < numberEvolve; i++) {
136         showUniverse(universe);
137         sleep(generationTime);
            // In the method nextGeneration(), row is 0 and col is 1
138         universe = nextGeneration(universe, fileName, row , col);
139     }
140 }

Next, you create an object called nextUniverse with the size of Cell[0][1]
However, you tried to access nextUniverse[38][58] in line 84 and hence a NullPointerException is thrown.

Basically, you cannot access nextUniverse[38][58] as the size of nextUniverse is [0][1]

77 private static Cell[][] nextGeneration(Cell[][] universe, String fileName, int row, int col) throws IOException {   
       // nextUniverse has a size of [0][1]
78     Cell[][] nextUniverse = new Cell[row][col];
79     for (int i = 1; i <= 38; i++) {
80         for (int j = 1; j <= 58; j++) {
81             int livingNeighbours = numberLivingNeighbours(universe);
82             if (universe[i][j] == Cell.LIVE) {
83                 if (livingNeighbours < 2 || livingNeighbours > 3) {
                       // Attempted to store values into nextUniverse[38][58]
84                     nextUniverse[i][j] = Cell.DEAD;
akgren_soar
  • 337
  • 4
  • 9