I tried to write a program such as the game of life, but without the sourcecode of the game of life. Just with the things I learned so far.
It is not quit clean, but when it works it is fine to me.
Right now I receive an error with NullPointerException.
This is my code:
import java.util.*; import java.io.*; class GameOfLife{ // main class Field field = new Field(); Scanner scanner; File source; String filename = "GameOfLifeInput.txt"; char[] charArray; public static void main(String[]args){ // starts up program new GameOfLife().play(); } // end of method void readInitial(String filename){ // reads input from file try{ source = new File(filename); scanner = new Scanner(source); String line = null; int row = 0; int col = 0; LineNumberReader lnr = new LineNumberReader(new FileReader(new File(filename))); lnr.skip(Long.MAX_VALUE); lnr.getLineNumber(); Cell[][] cells = new Cell[lnr.getLineNumber()+2][]; while (scanner.hasNext()){ line = scanner.nextLine(); char[] charArray = line.toCharArray(); cells[row] = new Cell[charArray.length+2]; for (Character c : charArray){ Cell cell = new Cell(); if (c == ' '){ cell.setAlive(false); }else{ cell.setAlive(true); } cells[row+1][col+1] = cell; // This is line 43 ++col; } ++row; } field.setField(cells); }catch(FileNotFoundException e){ System.out.println("Could not find or open file due to \n"+e); }catch (IOException e) {} } // end of method void play(){ //Asks user for number of generations and execute those generations with brakes of 2 secs in between them try{ int numberOfGenerations = 0; int i = 0; scanner = new Scanner(System.in); System.out.println("How many generations do you want to see?"); numberOfGenerations = scanner.nextInt(); readInitial(filename); for(i = 0; i < numberOfGenerations; i++){ field.print(); field.generationNextCalc(); } Thread.sleep(2000); }catch(InterruptedException e){ } } } // end of class class Field{ int i, j; Cell[][] cells; void setField(Cell[][] cells){ // method which replaces the empty matrix with the matrix with values read from file this.cells = cells; } void drawLine(){ //method to draw lines String s = "+"; for(int i = 1; i <= cells[0].length-1; i++){ s+="--"; } s+="+"; System.out.println(s); } // end of method public void print(){ //method to print generations drawLine(); for(int i = 1; i <= cells.length-1; i++){ System.out.print("|"); for(int j = 1; j <= cells[i].length-1; j++){ if(cells[i][j].isAlive()){ System.out.print("*"); }else{ System.out.print(" "); } } System.out.println("|"); } drawLine(); } // end of method int numNeighbours(int i, int j){ // method to count the number of neighbors int numNeighbours = 0; if(i>0){ if(cells[i-1][j].alive = true){ // Not top numNeighbours++; } } if(i<cells[0].length){ // Not bottem if(cells[i+1][j].alive = true){ numNeighbours++; } } if(j>0){ // Not left if(cells[i][j-1].alive = true){ numNeighbours++; } } if(j<cells.length){ // Not right if(cells[i][j+1].alive = true){ numNeighbours++; } } if(i > 0 && j > 0){ // If cell isn't on the left border and top border if(cells[i-1][j-1].alive = true){ numNeighbours++; } } if(i > 0 && j < cells.length-1){ // If cell isn't on the right border and top border if(cells[i-1][j+1].alive = true){ numNeighbours++; } } if(i < cells[0].length-1 && j > 0){ // If cell isn't on the bottem border and not on left border if(cells[i+1][j-1].alive = true){ numNeighbours++; } } if(i < cells[0].length-1 && j < cells.length-1){ // If the cell isn't on the right border and the bottem border if(cells[i+1][j+1].alive = true){ numNeighbours++; } } return numNeighbours; } // end of method void generationNextCalc(){ // method to count the next generation Cell cell = new Cell(); for (int i = 1; i < cells[0].length-1; i++){ for (int j = 1; j < cells.length-1; j++){ int numNeighbours = numNeighbours(i,j); if (numNeighbours < 2 || numNeighbours > 3){ cell.setAlive(false); }else if (numNeighbours == 3){ cell.setAlive(true); } } } /*for (int j = 1; j < cells.length-1; j++){ for (int i = 1; i < cells[0].length-1; i++){ generation[i][j] = generationNext[i][j]; } }*/ } // end of method } // end of class class Cell{ boolean alive = false; GameOfLife character = new GameOfLife(); boolean isAlive(){ return alive; } // end of method void setAlive(boolean state){ alive = state; } // end of method } // end of class
This is my output:
> run GameOfLife
How many generations do you want to see?
[DrJava Input Box user typed 2]
java.lang.NullPointerException
at GameOfLife.readInitial(GameOfLife.java:43)
at GameOfLife.play(GameOfLife.java:62)
at GameOfLife.main(GameOfLife.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:272)
>
How do I have to initialize it? I thought I already did!