Everything works, except my WritetoDisk, it gives me a NullPointerException PLEASE HELP!

/*
* database.java
*
* Created on March 28, 2008 by Karlee
*
* This program allows the user to add/remove students' first & last name, social security number, debt, and GPA
* The user can change te debt and GPA of the students
* The user can scan the list of students by name, debt, or GPA
*
*/
package database;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JOptionPane;


public class database {


private static ArrayList <String> myList;
public static void main(String[] args) {
//define ArrayList
ArrayList <student> myList = new ArrayList();
//define variables
int choose;
student s;
//menu
do{
choose = Integer.parseInt(JOptionPane.showInputDialog("Choose an option: " +
"\n1) Add a student to the list  \n2) Remove a student from the list" +
"\n3) Change the debt & GPA for a student \n4) Scan by last name" +
"\n5) Scan by first name \n6) Scan by debt \n7) Scan by GPA \n8) Print list " +
"\n9) Write to Disk \n10) Exit"));


switch (choose){


//add a new student
case 1: int SSN = Integer.parseInt(JOptionPane.showInputDialog("What is the SSN of the " +
"student you want to add?"));
s = new student("","",SSN,0.0,0.0);
if (myList.indexOf(s)==-1){
s.setFirst(JOptionPane.showInputDialog("What is the first name of the student?"));
s.setLast(JOptionPane.showInputDialog("What is the last name of the student?"));
s.setDebt(Double.parseDouble(JOptionPane.showInputDialog("What is the debt of " +
"the student?")));
s.setGPA(Double.parseDouble(JOptionPane.showInputDialog("What is the GPA of " +
"the student?\nGPA should be on a 4.0 scale")));
myList.add(s);
} else
JOptionPane.showMessageDialog(null, "There is already someone with that " +
"social security number");
break;
//remove a student
case 2: SSN = Integer.parseInt(JOptionPane.showInputDialog("What is the SSN of the " +
"student you want to remove?"));
s = new student("","",SSN,0.0,0.0);
if (myList.indexOf(s)==-1)
JOptionPane.showMessageDialog(null, "Sorry, there is no one in the list with " +
"that social security number");
else{
String decide = JOptionPane.showInputDialog("Are you sure you want to remove " +
"the person with SSN of " + SSN + " ?");
if (decide.equals("yes"))
myList.remove(s);
}
break;
//change the debt & GPA of a student
case 3:   SSN = Integer.parseInt(JOptionPane.showInputDialog("What is the SSN of the " +
"student you want to change the debt & GPA of?"));
s = new student("","",SSN,0.0,0.0);
int index=myList.indexOf(s);
if(index>=0){
double debt = Double.parseDouble(JOptionPane.showInputDialog("Change the debt " +
"to: "));
myList.get(index).setDebt(debt);
double gpa = Double.parseDouble(JOptionPane.showInputDialog("Change the GPA " +
"to: "));
myList.get(index).setGPA(gpa);
} else
JOptionPane.showMessageDialog(null, "Sorry, there is no one in the list with " +
"that SSN");
break;
//scan for last name
case 4: String last = JOptionPane.showInputDialog("What is the last name you want to " +
"search for?");
int count=0;
for(int i=0; i<myList.size(); i++){
if(myList.get(i).getLast().equals(last)){
count++;
JOptionPane.showMessageDialog(null, myList.get(i).getFirst() + " " + myList.get(i).getLast());
}
}
if(count==0)
JOptionPane.showMessageDialog(null, "Sorry, there is no one in the list with that " +
"last name");
break;
//scan for first name
case 5: String first = JOptionPane.showInputDialog("What is the first name you want to search " +
"for?");
count=0;
for(int i=0; i<myList.size(); i++)
if(myList.get(i).getFirst().equals(first)){
count++;
JOptionPane.showMessageDialog(null, myList.get(i).getFirst() + " " +
myList.get(i).getLast());
}
if(count==0)
JOptionPane.showMessageDialog(null, "Sorry, there is no one in the list with that " +
"first name");
break;
//scan for debt
case 6: double DEBT = Double.parseDouble(JOptionPane.showInputDialog("What minimum debt do " +
"you want to search for?"));
count=0;
for(int i=0; i<myList.size(); i++)
if(myList.get(i).getDebt()>=(DEBT)){
count++;
JOptionPane.showMessageDialog(null, myList.get(i).getFirst() + " " + myList.get(i).getLast() +
": " + myList.get(i).getDebt());
}
if(count==0)
JOptionPane.showMessageDialog(null, "Sorry, there is no one in the list with that " +
"debt");
break;
//scan for GPA
case 7: double GPAlow = Double.parseDouble(JOptionPane.showInputDialog("What minimum GPA do " +
"you want to search for?" +
"\nGPA should be on a 4.0 scale"));
double GPAhigh = Double.parseDouble(JOptionPane.showInputDialog("What maximum GPA do you " +
"want to search for?\nGPA should be on a 4.0 scale"));
count=0;
for(int i=0; i<myList.size(); i++){
if(myList.get(i).getGPA()>=(GPAlow) && myList.get(i).getGPA()<=(GPAhigh)){
count++;
JOptionPane.showMessageDialog(null, myList.get(i).getFirst() + " " +
myList.get(i).getLast() + ": " + myList.get(i).getGPA());
}
}
if(count==0)
JOptionPane.showMessageDialog(null, "Sorry, there is no one in the list with that " +
"GPA");
break;
//prints the list
case 8: for(int i=0; i<myList.size(); i++)
JOptionPane.showMessageDialog(null, myList.get(i));
break;
//writes the students to a file
case 9:  SSN = Integer.parseInt(JOptionPane.showInputDialog("What is the SSN of the" +
" student you want to write to disk?"));
s = new student("","",SSN,0.0,0.0);
try{
WriteToDisk("C:\\Documents and Settings\\Owner\\My Documents\\Karlee\\students.txt");
} catch (IOException ex){
System.out.println("Error Reading from Disk");
}
break;
}
//exits the program
}while(choose !=10);
System.exit(0);
}
//write to file method
public static void WriteToDisk(String Filename) throws IOException{
FileWriter in;
try {
in = new FileWriter( new File(Filename));
}catch (java.io.FileNotFoundException e) {
System.out.println("File: " + Filename + " not found.");
return;
}
//Read the words in one per line
BufferedWriter buffer = new BufferedWriter(in);
for(int i = 0; i < myList.size(); i++) {
buffer.write(myList.get(i));
buffer.newLine();
}
buffer.close();
}
}

Edited 3 Years Ago by happygeek: fixed formatting

Where exactly do you get that NullPointerException because I don't think that people will find it easy reading your whole code.
And it is probably because you are using an object that has not been instantiated. Use the "new" word.

Where exactly do you get that NullPointerException because I don't think that people will find it easy reading your whole code.
And it is probably because you are using an object that has not been instantiated. Use the "new" word.

The NullPointerException is in case 9:
WriteToDisk("C:\\Documents and Settings\\Owner\\My Documents\\Karlee\\students.txt");

and the for loop in BufferedWriter:
for(int i = 0; i < myList.size(); i++)

Then you need to make sure that the BufferedWriter is being created correctly and the "myList" has been initialized by the point you are calling that loop.

This article has been dead for over six months. Start a new discussion instead.