-3

EDIT

I have the input 5 3, where 10 is the amoung of characters per line and 5 (StartA) is the position of a * in that line.

I need to save this input in a boolean array so that it saves { false, false, true, false, false } Currently the program gives a nullPointerExeption when executing: CurrentState[i] == true

Can you, please, explain how I program this.

    class Program{
  Scanner sc = new Scanner(System.in);

  int L; // #characters per line
  int G; // # lines
  int Start; // Starting position
  int i; // position of character calculated
  int k; // Position in new line
  int j; // Row working
  String choice; // choice which program to execute
  boolean firstLine[] = new boolean[L+1];
  boolean nextGen[] = new boolean[L+1];

  void input() {
    L = sc.nextInt(); // # Characters per line
    G = sc.nextInt(); // # lines
    Start = sc.nextInt(); // Characters which is a * in first line
  }
  // output for first line
  void line() { 
    for (i=0; i<L+1; i++) {
      if (i == Start-1) {
        CurrentState[i] = true;
        System.out.print("*");
      } else if (i==L) {
        System.out.println("");
      } else {
        CurrentState[i] = false;
        System.out.print(".");
      }
    }
  }
  // rules for calculating the next line
  void rules() {
    for ( k=0; k<L-1; k++) {
      for ( i=1; i<L-1; i++) {
        if (CurrentState[i-1] == true && CurrentState[i]== true && CurrentState[i+1] == true) {
          nextGen[k] = false;
        } else if (CurrentState[i-1] == true && CurrentState[i] == true && CurrentState[i+1] == false) {
          nextGen[k] = true;
        } else if (CurrentState[i-1] == false && CurrentState[i] == true && CurrentState[i+1] == true) {
          nextGen[k] = true;
        } else if (CurrentState[i-1] == false && CurrentState[i] == false && CurrentState[i+1] == false) {
          nextGen[k] = false;
        } else if (CurrentState[i-1] == true && CurrentState[i] == false && CurrentState[i+1] == true) {
          nextGen[k] = true;
        } else if (CurrentState[i-1] == false && CurrentState[i] == false && CurrentState[i+1] == true) {
          nextGen[k] = true;
        } else if (CurrentState[i-1] == true && CurrentState[i] == false && CurrentState[i+1] == false) {
          nextGen[k] = true;
        } else if (CurrentState[i-1]== false && CurrentState[i] == true && CurrentState[i+1]== false) {
          nextGen[k] = false;
        }  
      }
      return true;
    }
  }
  // gives output for line 2 till G.
  void nextLine() {     
    for (j=0; j<G-1; j++){
      for (k=0; k<L; k++){
        if (nextGen[k] == true) {
          System.out.print("*");
        } else if (k==L-1) {
          System.out.println("");
        } else {
          System.out.print(".");
        }
      }
    } 
  }
  void run() {
    input();
    line();
    rules();
    nextLine();
  }

  public static void main(String[] args) {
    new Program().run();
  }
}
Peter
  • 13
  • 3
  • 1
    Did you initialize CurrentState? You need to declare the array like so before you can use it: `boolean CurrentState[] = new boolean[x]`, where `x` is the number of elements you want. – Mage Xy Sep 21 '15 at 13:25
  • Okay. And where do I need to put this line? – Peter Sep 21 '15 at 13:34
  • 1
    possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – khelwood Sep 21 '15 at 13:34
  • @Peter Either in the constructor of your class or at the property declaration. – Mage Xy Sep 21 '15 at 13:54

1 Answers1

1

You get a null pointer exception because the array of booleans is not initialised. You need to do something like:

boolean CurrentState[] = new boolean[L+1];

EDIT: The program below works. I tried with 8, 7 and 4 as input and I get this as output: ...*.... ....... ....... ....... ....... ....... .......

import java.util.Scanner;

class Program{
    Scanner sc = new Scanner(System.in);

    int L; // #characters per line
    int G; // # lines
    int Start; // Starting position
    int i; // position of character calculated
    int k; // Position in new line
    int j; // Row working
    String choice; // choice which program to execute
    boolean firstLine[];
    boolean nextGen[];
    boolean CurrentState[];

    void input() {
        L = sc.nextInt(); // # Characters per line
        G = sc.nextInt(); // # lines
        Start = sc.nextInt(); // Characters which is a * in first line
        CurrentState = new boolean[L+1];
        firstLine = new boolean[L+1];
        nextGen = new boolean[L+1];
    }
    // output for first line
    void line() {

        for (i=0; i<L+1; i++) {
            if (i == Start-1) {
                CurrentState[i] = true;
                System.out.print("*");
            } else if (i==L) {
                System.out.println("");
            } else {
                CurrentState[i] = false;
                System.out.print(".");
            }
        }
    }
    // rules for calculating the next line
    void rules() {
        for ( k=0; k<L-1; k++) {
            for ( i=1; i<L-1; i++) {
                if (CurrentState[i-1] == true && CurrentState[i]== true && CurrentState[i+1] == true) {
                    nextGen[k] = false;
                } else if (CurrentState[i-1] == true && CurrentState[i] == true && CurrentState[i+1] == false) {
                    nextGen[k] = true;
                } else if (CurrentState[i-1] == false && CurrentState[i] == true && CurrentState[i+1] == true) {
                    nextGen[k] = true;
                } else if (CurrentState[i-1] == false && CurrentState[i] == false && CurrentState[i+1] == false) {
                    nextGen[k] = false;
                } else if (CurrentState[i-1] == true && CurrentState[i] == false && CurrentState[i+1] == true) {
                    nextGen[k] = true;
                } else if (CurrentState[i-1] == false && CurrentState[i] == false && CurrentState[i+1] == true) {
                    nextGen[k] = true;
                } else if (CurrentState[i-1] == true && CurrentState[i] == false && CurrentState[i+1] == false) {
                    nextGen[k] = true;
                } else if (CurrentState[i-1]== false && CurrentState[i] == true && CurrentState[i+1]== false) {
                    nextGen[k] = false;
                }
            }
        }
    }
    // gives output for line 2 till G.
    void nextLine() {
        for (j=0; j<G-1; j++){
            for (k=0; k<L; k++){
                if (nextGen[k] == true) {
                    System.out.print("*");
                } else if (k==L-1) {
                    System.out.println("");
                } else {
                    System.out.print(".");
                }
            }
        }
    }
    void run() {
        input();
        line();
        rules();
        nextLine();
    }

    public static void main(String[] args) {
        new Program().run();
    }
}
Eduardo Sanchez-Ros
  • 1,777
  • 2
  • 18
  • 30