2013-04-24 23 views
0

我在我的战列舰项目中遇到问题。问题在于移动检查和制作。当我点击敌方委员会和计算机的轮到来时,我在hit = inShips [i] .checkMove(x,y)处得到空指针异常;在checkMove方法。在用户和计算机之间移动时出现战列舰运行时错误

以下是完整的项目:http://db.tt/V6YiTJVw(您需要更改在selectionPanel和图像路径selectionFrame类)的船级

感谢您的帮助

public void checkMove(JButton[][] inBoard, Ship[] inShips, int x, int y) { 
     boolean hit = false; 
     // Ship[] inShips = new Ship[5]; 
     int i = -1; 
     System.out.println("CHECKING MOVE AT CLASS PLAYBOARD (checkMove): "); 
     System.out.println("xCoord " + x); 
     System.out.println("yCoord " + y); 
     System.out.println(""); 
    // do { 
      i++; 
      System.out.println("INSHIPS: "); 
      System.out.println("!!!!!!!!!!!!!!!! " + i); 
      hit = inShips[i].checkMove(x, y); 
      System.out.println("IS HIT? : " + String.valueOf(hit)); 
    // } while ((i < 4) && (!hit)); 

checkMove:

public boolean checkMove(int x, int y) 
    { 
    for (int i = 0; i < this.size; i++) 
    { 
     if ((this.shipCoords[i][0] == x) && (this.shipCoords[i][1] == y)) 
     { 
     this.piecesHit += 1; 
     this.lastPieceHit[0] = x; 
     this.lastPieceHit[1] = y; 

     if (this.piecesHit == this.size) 
      this.sunk = true; 
     return true; 
     } 
    } 

    return false; 
    } 



public void getMove(int x, int y) { 
    if(this.computer == null) 
    checkMove(this.myBoard, this.myShips, x, y); 
    else 
    checkMove(this.myBoard, this.myShips, x, y); 

    if (this.myTurn == -1) 
     return; 


    this.myTurn = 1; 
    this.status.setText("Status: Waiting for your move."); 


} 










public void getMove(int x, int y) { 
     System.out.println("GETTING MOVE FROM COMPUTER CLASS: "); 
     System.out.println("%%%%%%%%%%%%COMPUTER CLASS X COORD FIRST COMES: " + x); 
     System.out.println("%%%%%%%%%%%%COMPUTER CLASS Y COORD FIRST COMES: " + y); 
     this.computerGameBoard.getMove(x, y); //Gets move from the player from PlayBoard class t through computerGameBoard variable. 

     int XCoord = this.compMoves[this.numMove][0]; 
     int YCoord = this.compMoves[this.numMove][2]; 
     System.out.println("[email protected]#*([email protected](#&*[email protected]*&#[email protected]*(#*([email protected]&*$^[email protected]&$^#[email protected]&*(#@!#[email protected]"); 
      for(int[] zz : compMoves) { 
       System.out.println(""); 

       System.out.println(Arrays.toString(zz)); 
      } 
     System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXCOORD: " + XCoord); 
     System.out.println("YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYCOORD: " + YCoord); 
     this.playerGameBoard.getMove(XCoord, YCoord); 
     this.computerGameBoard.makeMove(XCoord, YCoord); 
     this.numMove = numMove + 1; 
    } 



public void makeMove(int x, int y) 
    { 
     System.out.println("MAKING MOVE WITH COORDS: X " + x + " Y " +y); 
    checkMove(this.opponentBoard, this.opponentShips, x, y); 
    if (this.myTurn == -1) 
     return; 
    this.myTurn = 0; 
    this.status.setText("Status: Waiting for opponent move"); 


    if (this.computer != null) 
     this.computer.getMove(x, y); 
    } 
+4

那么你做了什么来诊断这个错误?你有没有验证'inShips'不是null?那inShips [i]'不是null? – 2013-04-24 17:49:51

回答

1

你的问题是一个常见的问题,我第一次看到与他人第一次使用对象数组秒。创建对象数组时,会创建该数组,但该数组的各个元素为null,即它们不引用对象。您创建数组后,你需要创建一个真正的对象:

Ship inShips[] = new Ship[5]; 
for(int i = 0; i < inShips.length; ++i) 
    inShips[i] = new Ship(); 

当你在对象上调用一个方法,需要有参考后面的真正对象。如果引用是null,它不引用对象。由于您正在调用null参考中的方法,因此您的程序崩溃并且您获得NullPointerException

+0

不,这不能解决错误 – Manos 2013-04-24 18:37:10

+0

看看Javadoc的NullPointerException:http://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.html – gparyani 2013-04-24 18:50:14

+0

我应该在哪里放置该代码?在checkMove方法里面? – Manos 2013-04-24 19:07:01