【问题标题】:Java board array troubleJava板阵列麻烦
【发布时间】:2013-12-05 10:34:24
【问题描述】:

我正在开发一个使用 ansi 代码创建战舰游戏的程序。 ansi 代码不是什么大问题,因为我发现这些代码允许我覆盖打印行。我的计划是用它把船放在已经画好的板上。我的问题在于放置船只的阵列(“0”)。似乎无法克服 nullpointerexception 错误。我觉得这是唯一阻止我完成程序的事情。任何帮助将不胜感激。

我的程序的一部分如下。

坐标类:

public class Coordinate {
   public String piece;
   int row, col, type;
   public boolean select, empty, hit, ship;
   public String [][] shipPiece;

   public String section()
   {
       piece = "i";

       empty = true;
       select = false;
       hit = false;
       //ship = false;

       if (empty)
       {
           piece = "\033[33m0\033[0m";
       }
       else if(!empty)
       {
           //empty = true;
           ship = true;
           piece = "0";
       }
       else if(!hit && ship)
           piece = "\033[33m0\033[0m";

       return piece;
   }

   public String placeShip()
   {
       for(int x = 0; x<=10; x++)
       {
           for(int y = 0; y<=10; y++)
           {
               shipPiece[x][y] = piece;
           }
       }
       return piece;   
   }
}

播放器类:

public class Player {

    private String name1, name2;
    //private ArrayList<Coordinate> moves;
    //private Coordinate move;
    private int playerNumber;
    Scanner in = new Scanner(System.in);
    Board bawd = new Board();
    Coordinate c = new Coordinate();
    Ship sh = new Ship();

    public Player(){}

    public void GetNames()
    {
        for(playerNumber = 1; playerNumber <= 2; playerNumber++)
        {
           System.out.print("Player " + playerNumber + ": ");
           name1 = in.nextLine();
           playerNumber++;
           System.out.print("Player " + playerNumber + ": ");
           name2 = in.nextLine();
           System.out.print("\033[2J");
        }
    }

    public void setShips()
    {
        bawd.singleP1View();
        sh.coordLoc();
        System.out.println("\033[2J");
        bawd.singleP2View();
        sh.coordLoc();
        System.out.println("\033[2J");
        game();
    }

    public void game()
    {
        bawd.p1turn();
        c.placeShip();
    }

}

【问题讨论】:

  • 你的空指针异常发生在哪一行?
  • Coordinate 类的第 41 行
  • 是的,这就清楚了。
  • 酷!但现在它给了我通常的arrayoutofbounds :(
  • 小问题,如果输出给我一个随机内存偏移量(即[[Ljava.lang.String;@b988a6),我想使一个toString方法正确吗?如果是这样,我将如何处理?

标签: java arrays nullpointerexception


【解决方案1】:

shipPiece array 未初始化。

改变

 public String [][] shipPiece;

 public String [][] shipPiece = new String [11][11];

【讨论】:

  • 好吧,这似乎解决了问题,感谢 David 和 Prabs :)
【解决方案2】:

这从未被初始化

public String [][] shipPiece;

你应该初始化这样的东西

public String [][] shipPiece = new String[10][10];

【讨论】:

    【解决方案3】:

    你永远不会初始化你的shipPiece。例如,您必须以这种方式对其进行初始化:

    public String [][] shipPiece = new String[11][11];
    

    【讨论】:

      【解决方案4】:
      public String [][] shipPiece;
      

      你没有初始化这个坏男孩!!!

      【讨论】:

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