2015-05-14 123 views
0

我收到一個NullPointerException錯誤,不能找出原因。我已經包括了所有我正在研究的三個類,並且////注意日食在哪裏說錯誤來自主方法(顯然有兩個位置)。Noob空指針異常錯誤

我是新來的編碼,從我理解這種情況發生時,當你試圖傳遞被認爲是空的東西,但我相信我試圖從代碼中傳遞新創建的魚。我相信這個錯誤很容易被有經驗的人看到。

謝謝!

import java.util.Scanner; 

public class FishTankManager { 

private static Scanner stdin = new Scanner(System.in); 
private static int userInput, userInput2; 
private static FishTank[] tanks = new FishTank[10]; 

public static void main (String[] args) { 
    while (true){ 
    System.out.println("Welcome to your new fish tank manager!\n"+ 
         "What would you like to do?\n"+ 
         "(1)Add a Fish\n(2)Remove a Fish\n"+ 
         "(3)Check a tank");   
    userInput = stdin.nextInt(); 

     if (userInput == 1){ 
      Fish fish = new Fish(); 
      changeTank(fish); //// Says its at here 
      continue; 
     } 
     else if(userInput ==2){ 
      removeFish(); 
     } 
     else{ 
      checkTank(); 
     } 
    } 
} 

private static void changeTank(Fish fish){ 
    System.out.println("Which tank would you like to put this fish in? (1-10)"); 
    userInput = stdin.nextInt(); 
    tanks[userInput-1].addFish(fish); ////and says its at here also 
} 

private static void removeFish(){ 
    System.out.println("Which tank would you like to remove the fish from? (1-10)"); 
    userInput = stdin.nextInt(); 
    System.out.println("Which fish would you like to flush down the toilet?"); 
    tanks[userInput-1].fishInTank(); 
    userInput2 = stdin.nextInt(); 
    tanks[userInput-1].flushFish(userInput2-1); 
} 

private static void checkTank(){ 
    System.out.println("Which tank would you like to check?"); 
    userInput = stdin.nextInt(); 
    tanks[userInput-1].fishInTank(); 
} 
} 

public class FishTank { 

private Fish[] tank; 
private int fishCount = 0; 

public FishTank(){ 
    this.tank = new Fish[5]; 
    this.fishCount = 0; 
} 

public void addFish(Fish fish){ 
    if (this.fishCount >=5){ 
     System.out.println("This tank is full! Try another"); 
      return; 
    } 
    else { 
     this.tank[fishCount] = fish; 
     this.fishCount++; 
    } 
} 

public void fishInTank(){ 
    for(int i=0; i<5; i++) 
     if (this.tank[i] == null){ 
      continue; 
     } 
     else{ 
     System.out.println("("+(i+1)+")"+this.tank[1].getName()); 
     } 
} 

public void flushFish(int f){ 
    this.tank[f] = null; 
} 
} 

import java.util.Scanner; 

public class Fish { 

private static Scanner stdin = new Scanner(System.in); 
private String userInput; 
private int userInput2; 
private boolean mean; 
private String name; 

public Fish(){  
    System.out.println("What is your fishes name?"); 
    userInput = stdin.next(); 
    this.name = userInput; 

    System.out.println("Is this fish aggressive?\n"+ 
         "(1)Yes\n(2)No");   
    userInput2 = stdin.nextInt(); 
    if (userInput2 == 1) 
     this.mean = true; 
    else 
     this.mean = false; 
} 

public String getName(){ 
    return this.name; 
} 

public boolean getMean(){ 
    return this.mean; 
} 
} 
+1

在哪一行你正在得到一個空指針異常? – Prudhvi

+0

堆棧跟蹤在哪裏?錯誤發生在哪裏,代碼的哪一行?錯誤在什麼情況下發生? – MadProgrammer

回答

1

tanks僅創建爲數組,但沒有產生任何FishTank。因此,tanks中的所有元素均爲null。所以這個:tanks[userInput-1].addFish(fish);將不起作用,因爲tanks[userInput - 1]null。對於這些位置:stacktrace會告訴您所有引發異常的方法。所以「它發生在這裏」和「也在這裏」實際上是「這個方法調用這個方法在這裏拋出異常」和「在這個方法中拋出異常」

+0

所以如果我使用for循環(int i = 0; i <10; i ++){坦克[i] = new FishTank}這應該解決我的問題?因爲我宣佈了坦克陣列,但我還沒有初始化它? – DojoOria

+0

取決於你在for循環中做什麼。最簡單的方法是將數組初始化爲for循環(我猜你的意思是這樣):'for(int i = 0; i Paul

+0

太棒了!這就是我正在編輯進我以前的評論!這個概念對我來說仍然是新的/令人困惑的......我覺得,因爲我已經宣佈這個數組爲FishTanks,所以它應該已經由它們組成了....不必單獨初始化它們只是讓我感到困惑,似乎雙重標準。 – DojoOria