【问题标题】:NullPointerException LifeGame.java ProgramNullPointerException LifeGame.java 程序
【发布时间】:2014-03-19 01:05:56
【问题描述】:

我已经为此工作了很长时间,但无法弄清楚为什么我会收到错误 线程“主”java.lang.NullPointerException 中的异常 在 LifeGame.cellValue(LifeGame.java:91) 在 Life.main(Life.java:31)

在我每次运行时输入文件名后。

这是主文件。 生活.java

 import java.util.*;  // Scanner
 import java.io.*;    // PrintStream


 public class Life {

    static PrintStream theScreen = new PrintStream(System.out);
    static Scanner theKeyboard = new Scanner(System.in);

    public static void main( String args[]) {

            theScreen.print("\nEnter the name of the initial configuration file: ");
            String inFile, str;
            inFile = theKeyboard.nextLine();

            // initialize the game
            LifeGame theGame = new LifeGame(inFile);


            int count = 0;
            while(true)
        {
                    // display current configuration
                theScreen.println(theGame.cellValue(0, 0));
                theScreen.println("\nGeneration " + count
                            + " - press 'Enter' for the next "
+ "generation or type 'done' to finish");
                    str = theKeyboard.nextLine();

                    if (str.equals("done")) break;

                    // generate next configuration
                    theGame.nextGeneration();
                    count++;
        }
    }
 }

这是 LifeGame.java

 import java.util.*; // Scanner
 import java.io.*;  // File

 public class LifeGame 
 {

    // define private variables here
int myRows=0;
int myCols=0;
int[][] myGrid;
static PrintStream theScreen = new PrintStream(System.out);

    // ++++++++++++++++++++++++++++++++++++++++++++++++++
    // * LifeGame constructor.                          *
    // * Receive: fileName, a string.                   *
    // * Precondition: fileName contains the name of    *
    // *   a file containing a Life configuration       *
    // *   (the number of rows, the number of columns,  *
    // *    and the values of each cell).               *
    // * Postcondition: myGrid has been initialized     *
    // *    to the configuration in fileName.           *
    // ++++++++++++++++++++++++++++++++++++++++++++++++++

    public LifeGame(String fileName)
    {
        Scanner theFile = null;


            try
            {
                theFile = new Scanner(new File(fileName));
            }
            catch (FileNotFoundException e)
            {
                System.err.println("File Not Found");
                System.exit(1);
            }
            myRows = theFile.nextInt();
            myCols = theFile.nextInt();
            int[][] myGrid = new int[myRows][myCols];
            for (int i = 0; i < myRows; i++)
                {
                    for (int j = 0; j < myCols; j++)
                        {
                            int num = theFile.nextInt();
                            myGrid[i][j] = num;
                        }
                }
            theFile.close();
            System.out.println(myRows+" "+myCols);
    }

    // ++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // * LifeGame columns extractor.                        *
    // * Return: the number of columns in my configuration. *
    // ++++++++++++++++++++++++++++++++++++++++++++++++++++++

    public int columns()
    {
        return myCols;
    }

    // ++++++++++++++++++++++++++++++++++++++++++++++++++*
    // * LifeGame rows extractor.                        *
    // * Return: the number of rows in my configuration. *
    // ++++++++++++++++++++++++++++++++++++++++++++++++++*

    public int rows()
    {
        return myRows;
    }

    // ++++++++++++++++++++++++++++++++++++++++++++++++++*
    // * LifeGame cell Value extractor.                  *
    // * Return: the value in cell [row][col]            *
    // ++++++++++++++++++++++++++++++++++++++++++++++++++*

    public int cellValue(int row,int col)
    {
        for (int i = 0; i < rows(); i++)
        {
            for(int j = 0; j < columns(); j++)
                {
                    if (myGrid[i][j] == '1')
                    {
                        theScreen.println("* ");
                    }
                    else
                    {
                        theScreen.println("  ");
                    }
                }
        System.out.println();
        }

    return 0;
    }

    // ++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // * Mutator to generate next LifeGame generation.      *
    // * Postcondition: For each cell myGrid[r][c]:         *
    // *   if myGrid[r][c] had 3 living neighbors:          *
    // *     myGrid[r][c] contains a 1.                     *
    // *   if myGrid[r][c] had less than 2 neighbors OR     *
    // *       myGrid[r][c] had more than 3 neighbors:      *
    // *     myGrid[r][c] contains a 0.                     *
    // ++++++++++++++++++++++++++++++++++++++++++++++++++++++

    public void nextGeneration()
    {
        int neighbors = 0;
        int tempGrid[][] = myGrid;
        for (int r = 0; r < myRows; r++)
            {
            for (int c = 0; c < myCols; c++)
                {
                 neighbors = (tempGrid[r-1][c-1]
                 + tempGrid[r-1][c]
                 + tempGrid[r-1][c+1]
                 + tempGrid[r][c-1]
                 + tempGrid[r][c+1]
                 + tempGrid[r+1][c-1]
                 + tempGrid[r+1][c]
                 + tempGrid[r+1][c+1]);
                 if ( neighbors == 3)
                     {
                    myGrid[r][c] = 1;
                     }
                 else if ( neighbors < 2 || neighbors > 3)
                    myGrid[r][c] = 0;

                }
            }
    }



    // ++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // * LifeGame toString function member.                 *
    // * override the toString method to display the array  *
    // * Return: a String containing my configuration.      *
    // ++++++++++++++++++++++++++++++++++++++++++++++++++++++
    public String toString()
    {
        return "";
    }

 } // end of the class

这是正在读取的文件

test.life

 5 5\n
 0 0 0 0 0\n
 0 0 1 0 0\n
 0 0 1 0 0\n
 0 0 1 0 0\n
 0 0 0 0 0\n

【问题讨论】:

  • theScreen.println(theGame.cellValue(0, 0));在 Life.java 中

标签: java exception nullpointerexception


【解决方案1】:

问题是myGridnull。如果您查看LifeGame(),您会注意到您正在重新定义myGrid 的本地版本,因此您没有正确设置成员变量。

变化:

int[][] myGrid = new int[myRows][myCols];

收件人:

myGrid = new int[myRows][myCols];

【讨论】:

    【解决方案2】:

    MyGrid 可能为空。您在构造函数中初始化名为 myGrid 的东西(仅在构造函数中进行作用域),而不是 self.myGrid(用于类)

    【讨论】:

      【解决方案3】:

      在 LifeGame.java 的第 40 行左右,您有:

      int[][] myGrid = new int[myRows][myCols];
      

      如果您尝试实例化实例变量 myGrid 而不是本地 myGrid,那么您应该取消 int[][]

      myGrid = new int[myRows][myCols];
      

      由于您从未实例化实例变量,因此稍后在 cellValue() 函数中调用它时它为 null。

      【讨论】:

        猜你喜欢
        • 2012-11-06
        • 1970-01-01
        • 1970-01-01
        • 2017-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-12
        • 1970-01-01
        相关资源
        最近更新 更多