Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: When I run my program, it says error: java.lang.NullPointerException

  1. #1
    Junior Member
    Join Date
    May 2013
    Location
    Tilburg
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question When I run my program, it says error: java.lang.NullPointerException

    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)
    >


  2. #2
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: When I run my program, it says error: java.lang.NullPointerException

    Null pointer means, that a variable on a corresponding line is set to null. Go to this line, locate all the non-primitive variables that are there and track, whether they are initialized or not.

  3. #3
    Junior Member
    Join Date
    May 2013
    Location
    Tilburg
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: When I run my program, it says error: java.lang.NullPointerException

    Quote Originally Posted by angstrem View Post
    Null pointer means, that a variable on a corresponding line is set to null. Go to this line, locate all the non-primitive variables that are there and track, whether they are initialized or not.
    Thank you for your fast reply!

    I tried to locate it, but I just don't see it. Only in this class the variabel numberOfGenerations is used and it is initialized just before the user is asked for the number of generations.

  4. #4
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: When I run my program, it says error: java.lang.NullPointerException

    Yes, you have to initialize variables. And, as I can see, you really have code for initializing variables. But the code is executed sequentially, statement after statement. If the statement on 61st line says, that "here, I have variable that is not initialized", then what is it supposed to mean?

  5. The Following User Says Thank You to angstrem For This Useful Post:

    ProgrammerNoobE (May 28th, 2013)

  6. #5
    Junior Member
    Join Date
    May 2013
    Location
    Tilburg
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: When I run my program, it says error: java.lang.NullPointerException

    Quote Originally Posted by angstrem View Post
    Yes, you have to initialize variables. And, as I can see, you really have code for initializing variables. But the code is executed sequentially, statement after statement. If the statement on 61st line says, that "here, I have variable that is not initialized", then what is it supposed to mean?
    Stupid of me! scanner = new Scanner (System.in);

    Thank you!

Similar Threads

  1. Replies: 1
    Last Post: February 27th, 2013, 06:48 AM
  2. Replies: 1
    Last Post: November 27th, 2012, 10:19 AM
  3. [SOLVED] Error Message: Exception in thread "main" java.lang.NullPointerException etc.
    By coffecupcake in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 15th, 2011, 09:34 AM
  4. [SOLVED] Java run time error: 'java.lang.NullPointerException"
    By ChandanSharma in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 20th, 2011, 11:53 AM
  5. Getting "AWT-EventQueue-0" java.lang.NullPointerException error
    By tryingtoJava in forum AWT / Java Swing
    Replies: 9
    Last Post: September 21st, 2009, 10:46 PM

Tags for this Thread