NullPointerException错误数组

时间:2016-03-06 18:20:25

标签: java arrays file nullpointerexception

尝试创建一个基本程序,其中创建对象Swimmer及其数据并将其存储到文件(游泳者的简单数据库)。但是,我遇到了许多运行时错误,其中许多错误被归类为NullPointerExceptions。我有一个没有构造函数的Swimmer类,其方法可以获取名称,年龄,性别等。此类还有返回所有这些信息的方法。在我的Tester类中,我创建了一个开关,提示用户显示Swimmers,Add Swimmer,Edit Swimmer,Delete Swimmer和退出程序。添加游泳者时会出现我当前的错误,特别是在设置名称时。 myList [maxIndex] .setName(a)中存在空指针异常。

这是我的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); 
}
}

这是我的Main类的代码:     import 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--;
}
}

我只需要帮助Null Pointer Exception。备份到文件的任何帮助也会很棒。我有一台Mac,我不认为这会过多地改变代码。 (也许只是文件扩展名)

1 个答案:

答案 0 :(得分:1)

这是因为你从未创造过任何新的游泳运动员。您的数组myList被声明为1000个游泳者的数组。但是,您的问题是您没有创建游泳者对象来占据这些位置,它们都是空的。因此,当您尝试设置游泳者的名字时,您会收到NullPointerException。

你应该像这样创造游泳运动员:

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