2016-03-06 78 views
-2

嘗試創建一個基本程序,在該程序中創建對象遊標及其數據並將其存儲到文件(游泳者的簡單數據庫)中。但是,我遇到了許多運行時錯誤,其中很多都被歸類爲NullPointerExceptions。我有一個沒有構造函數的Swimmer類,其中包含獲取姓名,年齡,性別等的方法。此類還具有返回所有這些信息的方法。在我的Tester類中,我創建了一個提示用戶顯示游泳者,添加游泳者,編輯游泳者,刪除游泳者和退出程序的開關。添加游泳運動員時出現當前錯誤,特別是在設置名稱時。在myList [maxIndex] .setName(a)處有一個空指針異常。NullPointerException錯誤數組

這是我的Swimmer類的代碼: import java.io. ; import java.util。;

public class Swimmer 
{ 
private String name; 
private int age; 
private int gradeLevel; 
private String gender; 
private int grade; 
private double fiftyFree; 
private double hundredFree; 
private double twoFree; 
private double fiveFree; 
private double hundredBack; 
private double hundredBreast; 
private double hundredFly; 
private double twoIM; 

Scanner kbReader = new Scanner(System.in); 

public void setName(String a) 
{ 
    name = a; 
} 

public String getName() 
{ 
    return name; 
} 

public void setAge(int a) 
{ 
    age = a; 
} 

public int getAge() 
{ 
    return age; 
} 

public void setGender(String a) 
{ 
    gender = a; 
} 

public String getGender() 
{ 
    return gender; 
} 

public void setGrade(int a) 
{ 
    grade = a; 
} 

public int getGrade() 
{ 
    return grade; 
} 

public void setFiftyFree(double a) 
{ 
    fiftyFree = a; 
} 

public double getFiftyFree() 
{ 
    return fiftyFree; 
} 

public void setHundredFree(double a) 
{ 
    hundredFree = a; 
} 

public double getHundredFree() 
{ 
    return hundredFree; 
} 

public void setTwoFree(double a) 
{ 
    twoFree = a; 
} 

public double getTwoFree() 
{ 
    return twoFree; 
} 

public void setFiveFree(double a) 
{ 
    fiveFree = a; 
} 

public double getFiveFree() 
{ 
    return fiveFree; 
} 

public void setHundredBack(double a) 
{ 
    hundredBack = a; 
} 

public double getHundredBack() 
{ 
    return hundredBack; 
} 

public void setHundredBreast(double a) 
{ 
    hundredBreast = a; 
} 

public double getHundredBreast() 
{ 
    return hundredBreast; 
} 

public void setTwoIM(double a) 
{ 
    twoIM = a; 
} 

public double getTwoIM() 
{ 
    return twoIM; 
} 

public void setHundredFly(double a) 
{ 
    hundredFly = a; 
} 

public double getHundredFly() 
{ 
    return hundredFly; 
} 

public void getInfo() 
{ 
    System.out.println("Name: " + name + "\n" + "Age: " + age + "/n" + "Grade  enter code here`Level: " + gradeLevel + "\n" + "Gender: " + gender); 
} 
} 

這是我的主類的代碼: 進口java.io. ; import java.util。; import java.util.Arrays;

public class Tester 
{ 
public static Swimmer[] myList = new Swimmer[1000]; 
public static int maxIndex = 0; 

public static void main(String args[]) throws IOException 
{ 
    Scanner kbReader = new Scanner(System.in); 

    int choice; 
    String swimmerName = "Default"; 
    boolean condition = true; 

    while(condition) 
    { 
     System.out.println("Which of the following would you like to do?"); 
     System.out.println("1. Show Swimmers" + "\n" + "2. Add Swimmer" + "\n" + "3. Edit Swimmer" + "\n" + "4. Delete Swimmer" + "\n" + "5. Exit"); 
     choice = kbReader.nextInt(); 

     switch (choice) 
     { 
      case 1: 
      showSwimmer(); 
      break; 
      case 2: 
      addSwimmer(); 
      break; 
      case 3: 
      editSwimmer(); 
      break; 
      case 4: 
      deleteSwimmer(); 
      break; 
      case 5: 
      condition = false; 
      break; 
     } 
    } 
    FileWriter(); 
} 

public static void FileWriter() throws IOException 
{ 
    FileWriter fw = new FileWriter("database.txt"); 
    PrintWriter output = new PrintWriter(fw); 

    for(int j = 0; j<=maxIndex; j++) 
    { 
     output.println(myList[j].getName() + " ~ " + myList[j].getAge() + " ~  " + myList[j].getGender() + " ~ " + myList[j].getGrade() + " ~ " +  myList[j].getFiftyFree() + " ~ " + myList[j].getHundredFree() + " ~ " +  myList[j].getTwoFree() + " ~ " + myList[j].getFiveFree() + " ~ " +  myList[j].getHundredBack() + " ~ " + myList[j].getHundredBreast() + " ~ " +  myList[j].getHundredFly() + myList[j].getTwoIM()); 
    } 

    output.close(); 
    fw.close(); 
} 

public static void FileReader() throws IOException 
{ 
    Scanner sf = new Scanner(new File("database.txt")); 
    String s, sp[]; 
    while(sf.hasNext()) 
    { 
     maxIndex++; 
     s = sf.nextLine(); 
     sp = s.split("~"); 
     myList[maxIndex] = new Swimmer(); 
    } 
    sf.close(); 

} 

public static void swap(int a, int b) 
{ 
    Swimmer temp = myList[a]; 
    myList[a] = myList[maxIndex]; 
    myList[maxIndex] = temp; 



} 

public static void showSwimmer() 

{ 
    for(int j = 1; j < maxIndex; j++) 
    { 
     System.out.println((j) + ". " + myList[j].getName()); 
    } 
} 

public static void addSwimmer() 
{ 
    Scanner kbReader = new Scanner (System.in); 

    System.out.println("What is the swimmer's name? "); 
    String a = kbReader.nextLine(); 
    myList[maxIndex].setName(a); 

    System.out.println("Is the swimmer male or female? Please enter \"m\" for  male and \"f\" for female. "); 
    String b = kbReader.nextLine(); 

    System.out.println("How old is he/she? "); 
    int c = kbReader.nextInt(); 
    myList[maxIndex].setAge(c); 

    System.out.println("What is the numerical value of the swimmer's grade?"); 
    int d = kbReader.nextInt(); 
    myList[maxIndex].setGrade(d); 

    System.out.println("How many minutes does it take for this swimmer to  complete the 50 Freestyle?"); 
    double e = kbReader.nextDouble(); 
    System.out.println("How many additional seconds does it take for this  swimmer to complete the 50 freestyle?"); 
    double f = kbReader.nextDouble(); 
    double g = (e*60.0) + f; 
    myList[maxIndex].setFiftyFree(g); 

    System.out.println("How many minutes does it take for this swimmer to  complete the 100 Freestyle?"); 
    double h = kbReader.nextDouble(); 
    System.out.println("How many additional seconds does it take for this  swimmer to complete the 100 Freestyle?"); 
    double i = kbReader.nextDouble(); 
    double j = (h*60.0) + i; 
    myList[maxIndex].setHundredFree(j); 

    System.out.println("How many minutes does it take for this swimmer to  complete the 200 Freestyle?"); 
    double k = kbReader.nextDouble(); 
    System.out.println("How many additional seconds does it take for this  swimmer to complete the 200 Freestyle?"); 
    double l = kbReader.nextDouble(); 
    double m = (k*60.0) + l; 
    myList[maxIndex].setTwoFree(k); 

    System.out.println("How many minutes does it take for this swimmer to  complete the 500 Freestyle?"); 
    double n = kbReader.nextDouble(); 
    System.out.println("How many additional seconds does it take for this  swimmer to complete the 200 Freestyle?"); 
    double o = kbReader.nextDouble(); 
    double p = (n*60.0) + o; 
    myList[maxIndex].setFiveFree(p); 

    System.out.println("How many minutes does it take for this swimmer to  complete the 100 Backstroke?"); 
    double q = kbReader.nextDouble(); 
    System.out.println("How many additional seconds does it take for this  swimmer to complete the 100 Backstroke?"); 
    double r = kbReader.nextDouble(); 
    double s = (q*60.0) + r; 
    myList[maxIndex].setHundredBack(s); 

    System.out.println("How many minutes does it take for this swimmer to  complete the 100 Breastroke?"); 
    double t = kbReader.nextDouble(); 
    System.out.println("How many additional seconds does it take for this  swimmer to complete the 100 Breastroke?"); 
    double u = kbReader.nextDouble(); 
    double v = (t*60.0) + u; 
    myList[maxIndex].setHundredBreast(v); 

    System.out.println("How many minutes does it take for this swimmer to  complete the 100 Butterfly?"); 
    double w = kbReader.nextDouble(); 
    System.out.println("How many additional seconds does it take for this  swimmer to complete the 100 Butterfly?"); 
    double x = kbReader.nextDouble(); 
    double y = (w*60.0) + x; 
    myList[maxIndex].setHundredFly(y); 

    System.out.println("How many minutes does it take for this swimmer to  complete the 200 IM?"); 
    double z = kbReader.nextDouble(); 
    System.out.println("How many additional seconds does it take for this  swimmer to complete the 200 IM?"); 
    double aa = kbReader.nextDouble(); 
    double bb = (z*60.0) + aa; 
    myList[maxIndex].setTwoIM(bb); 
    maxIndex++; 
} 

public static void editSwimmer() 
{ 
    Scanner kbReader = new Scanner(System.in); 
    System.out.println("Which swimmer would you like to edit? Please enter the  corresponding number."); 
    System.out.println(Arrays.toString(myList)); 
    int choice = kbReader.nextInt(); 
} 

public static void deleteSwimmer() 
{ 
    int a; 
    Scanner kbReader = new Scanner(System.in); 
    System.out.println("Which swimmer would you like to delete? Please enter  the corresponding number."); 
    System.out.println(Arrays.toString(myList)); 
    a = kbReader.nextInt(); 

    swap(a-1, maxIndex); 
    maxIndex--; 
} 
} 

我只是需要空指針異常的幫助。備份到文件的任何幫助也會很好。我有一臺蘋果機,我不認爲這會改變代碼太多。 (也許只是文件擴展名)

+0

發佈了錯誤的完整堆棧跟蹤將是有益的。 – Michael

回答

1

這是因爲你從來沒有創建任何新的游泳運動員。你的數組myList被聲明爲1000個游泳者的數組。但是,你的問題是你沒有創建游泳對象來佔據這些位置,它們都是空的。所以,當你嘗試設置游泳者的名字時,你會得到一個NullPointerException。

您應該創建游泳這樣:

for (int i = 0; i < 1000; i++) { 
    myList[i] = new Swimmer(); 
}