2015-11-04 118 views
1

我正嘗試在Java中創建一個用戶提示輸入行寬和列長度的2D遊戲。 A「世界」對象被創建,看起來像這樣印刷時(其中P是玩家的角色):Java 2D遊戲NullPointerException

P--- 
---- 
---- 
---- 

然後,用戶可以輸入上,下,左,右或退出應圍繞移動P和如果移動將角色從地圖上移開,則什麼也不做。我的問題是在我進入的寬度和長度,我得到以下錯誤:

java.lang.NullPointerException 
at World.displayWorld(Driver.java:90) 
at Driver.main(Driver.java:28) 

我認爲,這意味着Java是看到worldDimensions.length爲空/空?我認爲當世界對象被創建時,我正在爲它賦值。任何指導都非常感謝!

這裏是我的代碼:

import java.util.Scanner; 

public class Driver { 
    public static void main(String args[]) { 

     //Create a scanner object 
     Scanner input = new Scanner(System. in); 

     boolean exit = true; 

     //Prompt user to enter world width and height 
     System.out.print("World width: "); 
     int userWidth = input.nextInt(); 
     System.out.print("World height: "); 
     int userHeight = input.nextInt(); 

     //Create a world object 
     World world1 = new World(userWidth, userHeight); 
     world1.displayWorld(); 

     System.out.println("Possible inputs: up, down, left, right, exit"); 

     while (exit = true) { 
      //display world 
      world1.displayWorld(); 

      System.out.print("ACTION > "); 
      String userAction = input.next(); 

      if (userAction == "up" || userAction == "down" || userAction == "left" || userAction == "right" || userAction == "exit") { 
       switch (userAction) { 
        case "up": 
         world1.moveUp(); 
         break; 
        case "down": 
         world1.moveDown(); 
         break; 
        case "left": 
         world1.moveLeft(); 
         break; 
        case "right": 
         world1.moveRight(); 
         break; 
        case "exit": 
         exit = false; 
         break; 
       } 
      } else { 
       System.out.println("Invalid Input. Possible inputs are: up, down, left, right, or exit."); 
      } 
     } 

    } 
} 

class World { 
    static private char[][] worldDimensions; 
    static private int characterRow; 
    static private int characterColumn; 

    public World(int userWidth, int userHeight) { 
     char[][] worldDimensions = new char[userHeight][userWidth]; 
     int characterRow = 0; 
     int characterColumn = 0; 

     for (int row = 0; row < worldDimensions.length; row++) { 
      for (int column = 0; column < worldDimensions[row].length; column++) { 
       if (characterRow == row && characterColumn == column) { 
        worldDimensions[row][column] = (char) 
        'P'; 
       } else { 
        worldDimensions[row][column] = (char) 
        '-'; 
       } 
      } 
     } 
    } 

    public void displayWorld() { 
     for (int row = 0; row < worldDimensions.length; row++) { 
      for (int column = 0; column < worldDimensions[row].length; column++) { 
       System.out.print(worldDimensions[row][column]); 
      } 
      System.out.println(); 
     } 
    } 

    public void moveUp() { 
     if (characterRow - 1 >= 0) { 
      characterRow--; 
     } 
    } 

    public void moveDown() { 
     if (characterRow + 1 <= worldDimensions[0].length) { 
      characterRow++; 
     } 
    } 

    public void moveLeft() { 
     if (characterColumn - 1 >= 0) { 
      characterColumn--; 
     } 
    } 

    public void moveRight() { 
     if (characterRow + 1 <= worldDimensions.length) { 
      characterRow++; 
     } 
    } 
} 
+2

使用['equals()方法來比較strings'(http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in- JAVA)。還要檢查['什麼是空指針異常,以及如何解決它?'](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do -i-fix-it) – sam

+0

爲了將來的參考,它有用於指示空指針在哪一行 - 而不是我們必須找到它。 – Sh4d0wsPlyr

+3

這裏至少有三個錯誤 - 使用'='而不是* *'exit',使用'=='比較字符串,並將冗餘轉換爲'char' ... – Makoto

回答

1

你遮蔽worldDimensions你的構造函數裏面。本地聲明不會像以前聲明的那樣爲您提供相同的字段。

只需刪除的聲明,你會好起來的(至少是對於那些錯誤):

worldDimensions = new char[userHeight][userWidth]; 
1

你覆蓋類變量worldDimensions。你的世界構造看起來應該像下面

public World(int userWidth, int userHeight) 
{ 
//here is where you were overwriting the global variable, leaving it null 
//and populating the local one instead 
worldDimensions = new char[userHeight][userWidth]; 
int characterRow = 0; 
int characterColumn = 0; 

for (int row = 0; row < worldDimensions.length; row++) 
    { 
    for (int column = 0; column < worldDimensions[row].length; column++) 
     { 
     if (characterRow == row && characterColumn == column) 
      { 
      worldDimensions[row][column] = (char)'P'; 
      } 
     else 
      { 
      worldDimensions[row][column] = (char)'-'; 
      } 
     } 
    } 
}