Java新手:如何解决NullPointerException?

时间:2012-10-16 21:24:35

标签: java nullpointerexception

我试图在Java中创建Conway的生活游戏,似乎已经碰到了一堵砖墙。 我创建了一个名为“Cell”的类,它包含一个布尔变量,它基本上确定单元格是活着还是死亡,以及在需要时杀死或创建单元格的方法。在我的main方法中,我获取用户在游戏中想要的行数和列数,并尝试创建一个名为“location”的Cell对象数组。当我尝试运行代码并打印每个单元格的初始值时,我得到一个空指针异常错误,我不知道如何解决它。据我所知,每个数组都不应该有空值,但我对此很新...

//Create a Cell class for the purpose of creating Cell objects.

public class Cell

{


private boolean cellState;

//Cell Constructor. Initializes every cell's state to dead.
public Cell()
{
    cellState = false;
}

//This function kills a cell.
//Should be called using objectName[x][y].killCell.
public void killCell()
{
    cellState = false;
}

//This function creates a cell.
//Should be called using objectName[x][y]createCell.
public void createCell()
{
    cellState = true;
}

public void printCell()
{
    if (cellState == true)
            {  
               System.out.print("1");
            }
            else if
            {
               System.out.print("0");
            }

}


//End Class Cell//
}

这是我的Cell课程。如果一个细胞存活,将在其位置打印一个细胞。如果死了,就会有0代替。

这是我的主要方法。错误发生在我创建Cell对象数组的行。我做错了吗?

//GAME OF LIFE//


import java.util.Scanner;
import java.lang.*;
public class GameOfLife
{
    public static void main(String[] args)
    {
    Scanner scan = new Scanner(System.in);
    System.out.println("\t\tWelcome to the Game of Life!");
    System.out.println("\n\nDeveloped by Daniel Pikul");

    System.out.print("\n\nHow many rows would you like your game to have?");
    int numRows = scan.nextInt();

    System.out.print("How many columns would you like your game to have?");
    int numColumns = scan.nextInt();

    //Create an array of cell objects.
    Cell[][] location = new Cell[numColumns][numRows];

    //This for loop will print out the cell array to the screen.//
    for (int i = 0; i < numRows; i++)
    {
        for (int j = 0; j < numColumns; j++)
        {
            location[i][j].printCell();
        }
        System.out.print("\n");
    }

    //Prompt the user to enter the coordinates of cells that should live.
    System.out.println("Input coordinates tocreate active cells.");

    int xCo, yCo;
    char userChoice;

    //This do loop takes coordinates from the user. Every valid coordinate
    //creates a living cell in that location.
    do
    {
        System.out.print("Enter an x coordinate and a y coordinate: ");
        xCo = scan.nextInt();
        yCo = scan.nextInt();
        location[xCo][yCo].createCell();
        System.out.print("Enter another coordinate? (Y/N) ");
        String tempString = scan.next();
        userChoice = tempString.charAt(0);
    }while(userChoice == 'Y');

    //THIS IS AS FAR AS I HAVE GOTTEN IN THE PROGRAM THUS FAR//
}

}

3 个答案:

答案 0 :(得分:3)

//This for loop will print out the cell array to the screen.//
for (int i = 0; i < numRows; i++)
{
    for (int j = 0; j < numColumns; j++)
    {
        location[i][j].printCell();  // location[i][j] not instantiated
    }
    System.out.print("\n");
}

在上面的for循环中: - 你还没有在你的数组中实例化你的Cell对象: -

location[i][j].printCell();

在此代码之前,您需要执行以下操作: -

location[i][j] = new Cell();

答案 1 :(得分:1)

public void printCell()
{
    if (cellState == true)
            {  
               System.out.print("1");
            }
            else if
            {
               System.out.print("0");
            }

}

要:

public void printCell()
{
    System.out.print( cellState ? "1" : "0" );
}

如果在那里你有额外的。

我们认真地确实需要您的错误日志来为您调试,请大家帮个忙,请粘贴该错误日志。

答案 2 :(得分:0)

java.lang.RuntimeException:无法编译的源代码 这会给你一个运行时异常我猜。

像“ java.lang.RuntimeException:Uncompilable source code ”之类的东西 您刚刚在其他地方添加了 if

   public void printCell()
   {
        if (cellState == true){
                 System.out.print("1");
        }
        else /*if*/
        {
           System.out.print("0");
        }

   }

我评论过它。立即试用代码并运行它。或者在 else if(cellState == false)中给出一些条件,在这种情况下没有任何意义,因为布尔可以有true或false,但仍然希望给它像这样对你有用...因为如果您使用 if ,则必须提供某些条件才能正常工作..