2013-10-24 18 views
0

我一直使用我的類獲取空指針異常。使用類數組的空指針異常

客戶端類:

public static void main(String[] args) { 
    Scanner kb = new Scanner(System.in); 
    int count = 0; 

    for (int i = 0; i < 100; i++){ 

     count ++; 
     System.out.println(i); 
     Clip[] newClip = new Clip[100]; 
     newClip[i] = new Clip(); 
     menu(); 
     int option = kb.nextInt(); 

     switch(option){ 
      case 1: System.exit(0); 
        break; 

      case 2: newClip[i].Input(); 
        newClip[i].Output(); 
        break; 

      case 3: int indexclip = 0; 
        int testclip = 0; 
        Scanner key = new Scanner(System.in); 
        System.out.println("Enter the index number of the clip: "); 
        testclip = key.nextInt(); 

        for (int j = 0; j < 100; j++){ 

         indexclip = newClip[j].getIndex(); // happens here 
         System.out.println(indexclip); 
         if(testclip == indexclip){ 
          j = 120; 
          newClip[j].Output(); // and i would assume here too 
         } 
        } 
        break; 
     } 
    } 
} 

裁剪類:

import java.util.*; 

public class Clip { 

private int index; 
private String surname;   
private float length; 
private float speed; 
private String time = "testing"; 

public Clip(){ 
    index = 0; 
    surname = "N/A"; 
    length = (float) 0.00; 
    speed = (float) 0.00; 
    time = "0:00AM";   
} 

public void Output(){ 
    System.out.println("Index: "+ index); 
    System.out.println("Surname: " + surname); 
    System.out.println("Length: " + length); 
    System.out.println("Speed: "+ speed + "m/s"); 
    System.out.println("Time: "+ time); 


} 
public void Input(){ 
    int testint; 
    float testfloat; 
    int spacePos; 
    String testString; 

    Scanner kb = new Scanner(System.in); 
    Scanner key = new Scanner(System.in); 

    System.out.println("Input an index number between 1 - 10000: "); 
    testint = kb.nextInt(); 
    for (int i = 0; i < 100; i++){ 
     if (testint < 1 || testint > 10000){ 

      System.out.println("Input an index number between 1 - 10000: "); 
      testint = kb.nextInt(); 

      } 

     else { 
      i = 120; 
     } 
    } 
    index = testint; 

    System.out.println("What is the competitor's Surname and their Given name: "); 
    surname = key.nextLine(); 

    System.out.println("Length of the Tape in seconds: "); 
    testfloat = kb.nextFloat(); 
    for (int i = 0; i < 100; i++){ 
     if (testfloat < 1 || testfloat > 60){ 

      System.out.println("Length of the Tape in seconds: "); 
      testfloat = kb.nextFloat(); 

      } 
     else { 
      i = 120; 
     } 
    } 
    length = testfloat; 

    System.out.println("Estimated Speed of competitor in metres per second: "); 
    testfloat = kb.nextFloat(); 
    for (int i = 0; i < 100; i++){ 
     if (testfloat < 7 && testfloat > 13){ 

      System.out.println("Estimated Speed of competitor in metres per second: "); 
      testfloat = kb.nextInt(); 

      } 
     else { 
      i = 120; 
     } 
    } 
    speed = testfloat; 

    System.out.println("Time of recording between 0900 - 1700: "); 
    testString = key.nextLine(); 

    for (int i = 0; i < 100; i++){ 
     if (testString.length() != 4){ 
      System.out.println("Time of recording between 09:00 - 17:00: "); 
      testString = key.nextLine(); 
     } 
     else { 
      i = 120; 
     } 
    } 
    time = testString; 
} 

public int getIndex(){ 
    return index; 
} 

public String getSurname(){ 
    return surname; 
} 

public float getLength(){ 
    return length; 
} 

public float getSpeed(){ 
    return speed; 
} 

public String getTime(){ 
    return time; 
} 

據我所知,剪輯類必須被初始化,我已經做了,也和我一樣運行選項2首先使其他類的構造函數不被使用。我只是希望它在索引讀取數量和掃描匹配的索引號

剪輯陣列的任何援助將有助於大的時候

歡呼

+0

發佈異常的堆棧跟蹤,它會告訴你哪一行導致了問題,而這又會很容易地告訴你什麼是你期望不會出現的'null'。 –

回答

4

你需要移動的聲明你的陣列外的首先爲循環。

Clip[] newClip = new Clip[100]; 
for (int i = 0; i < 100; i++){ 
/**/ 
} 

因爲當你在情況下,你switch聲明,3你通過所有的陣列Clip的(元素循環,但是因爲你只設置數組的一個元素,數組的99個要素實際上設置爲null)。

1

問題是你正在循環中每次迭代創建數組,所以只有當前條目(i)不是null

爲了解決這個問題,招行:

Clip[] newClip = new Clip[100]; 

需求是循環(for語句前)之外。

另外,在int j的循環中,這隻有在i == 100時纔有效。不確定這是否有保證。否則,在newClip,是經過我的條目也將null

0

的NPE是在這裏:

Clip[] newClip = new Clip[100]; 

您需要獲得該行出你的循環。