Hello,
I need some help, because I just don't see it!
This is my code:
class GameOfLife{ // main class Field field = new Field(); int numberOfGenerations; Scanner scanner; File source; String filename = "GameOfLifeInput.txt"; char[] charArray; public static void main(String[]args){ // starts up program new GameOfLife().play(); //This is line 18 } // end of method void readInitial(String filename){ // reads input from file try{ scanner = new Scanner(source); String line = null; source = new File(filename); 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()][]; while (scanner.hasNext()){ line = scanner.nextLine(); char[] charArray = line.toCharArray(); cells[row] = new Cell[charArray.length]; for (Character c : charArray){ Cell cell = new Cell(); if (c == ' '){ cell.setAlive(false); }else{ cell.setAlive(true); } cells[row][col] = cell; ++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(){ //displays the generations try{ int i = 0; numberOfGenerations = 0; System.out.println("How many generations do you want to see?"); numberOfGenerations = scanner.nextInt(); // this is line 61 readInitial(filename); while(i < numberOfGenerations){ field.print(); i++; } Thread.sleep(2000); }catch(InterruptedException e){ } } } // end of class
This is the output:
Welcome to DrJava. Working directory is C:\Users\Esteban\Documents\School\2IS60 - App Programming
> run GameOfLife
How many generations do you want to see?
java.lang.NullPointerException
at GameOfLife.play(GameOfLife.java:61)
at GameOfLife.main(GameOfLife.java:18)
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)
>