-2

Trying to create a basic program, in which an object Swimmer and its data are created and stored to a file (simple database for swimmers). However, I am experiencing numerous run time errors, many of which are classified as NullPointerExceptions. I have a Swimmer class w/o a Constructor with methods that get names, ages, genders, etc. This class also has methods to return all this information. In my Tester class, I have created a switch that prompts the user to either Show Swimmers, Add Swimmer, Edit Swimmer, Delete Swimmer, and Exit the Program. My current error appears when adding a swimmer, specifically when setting a name. There is a Null Pointer Exception at myList[maxIndex].setName(a).

This is my code for my Swimmer class: 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); 
}
}

And this is the code for my Main class: 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--;
}
}

I just need help with the Null Pointer Exception. Any help with backing up to a file would also be great. I have a Mac, I don't think this should alter the code too much. (Maybe just the file extension)

1 Answers1

1

This is because you have never created any new swimmers. Your array, myList, is declared to be an array of 1000 swimmers. However, your problem is that you have not created swimmer objects to take up those positions, they are all null. So, when you try to set the name of the swimmer, you get a NullPointerException.

You should create the swimmers as such:

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