Hi,
I am getting a null pointer exception with the code below. From what I have read, it seems like something is null, but I do not understand how this could be a problem? The error is occurring on line 205 of the StudentContainer class. (This is at the very bottom of the code.)
The problem is occurring when I call the parse method of the Student class from the StudentContainer class. Am I setting up the Student objects incorrectly?
Sorry for the large amount of code.
public class StudentRecords {
public static void main(String[] args) {
new StudentContainer();]
public class Student {
String firstName = " ";
String lastName = " ";
String addressLine1 = " ";
String addressLine2 = " ";
String city = " ";
String state = " ";
String zip = " ";
String bdayMonth = " ";
String bdayDay = " ";
String bdayYear = " ";
String gdayMonth = " ";
String gdayDay = " ";
String gdayYear = " ";
String gpa = " ";
String creditHours = " ";
//constructor deleted. I have tried declaring variables here, still fails.
public void parse(String line){
System.out.println("Student parse method");
firstName.equals(line.split(","));
System.out.println();
lastName.equals(line.split(","));
System.out.println();
addressLine1.equals(line.split(","));
System.out.println();
addressLine2.equals(line.split(","));
System.out.println();
city.equals(line.split(","));
System.out.println();
state.equals(line.split(","));
System.out.println();
zip.equals(line.split(","));
System.out.println();
bdayMonth.equals(line.split(","));
System.out.println();
bdayDay.equals(line.split(","));
System.out.println();
bdayYear.equals(line.split(","));
System.out.println();
gdayMonth.equals(line.split(","));
System.out.println();
gdayDay.equals(line.split(","));
System.out.println();
gdayYear.equals(line.split(","));
System.out.println();
gpa.equals(line.split(","));
System.out.println();
creditHours.equals(line.split(","));
System.out.println();
}
}
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class StudentContainer extends JFrame implements ActionListener {
public int counter = 0;
// public String[][] asciiPlots = new String[64][FRAMES]; //two dimensional array keeps track of changes to TextBoxes
private static final long serialVersionUID = 1L;
final int num_students = 50;
Student students[] = new Student[num_students];
String line[] = new String [50];
// public static final int NUMROWS = 8;
// public static final int FRAMES = 10; //program is limited to 10 frames
JLabel lblFirstName = new JLabel(" First Name:"); //creates lblName label
JLabel lblLastName = new JLabel(" Last Name:"); //creates lblAddress label
JLabel lblAddressLine1 = new JLabel(" Address Line 1:"); //creates lblAddress label
JLabel lblAddressLine2 = new JLabel(" Address Line 2:"); //creates lblAddress label
JLabel lblCity = new JLabel(" City:"); //creates lblPhone label
JLabel lblState = new JLabel(" State:"); //creates lblAddress label
JLabel lblZip = new JLabel(" Zip Code:"); //creates lblAddress label
JLabel lblBirthday = new JLabel(" Date of Birth:"); //creates lblAddress label
JLabel lblGradDate = new JLabel(" Graduation Date:"); //creates lblAddress label
JLabel lblGpa = new JLabel(" GPA:"); //creates lblAddress label
JLabel lblCreditHours = new JLabel(" Credit Hours:"); //creates lblAddress label
JButton clickSave = new JButton("Save"); //creates Save button
JButton clickPrevious = new JButton("Previous"); //creates Previous button
JButton clickNext = new JButton("Next"); //creates Next button
JButton clickDelete = new JButton("Delete"); //creates Delete button
JTextField FirstNameInput = new JTextField(""); //creates NameInput text field
JTextField LastNameInput = new JTextField(""); //creates AddressInput text field
JTextField AddressLine1Input = new JTextField("");
JTextField AddressLine2Input = new JTextField("");
JTextField CityInput = new JTextField("");
JTextField StateInput = new JTextField("");
JTextField ZipInput = new JTextField("");
JTextField BirthdayInput = new JTextField("");
JTextField GradDateInput = new JTextField("");
JTextField GpaInput = new JTextField("");
JTextField CreditHoursInput = new JTextField("");
//creates PhoneInput text field //creates 64 text boxes for 8x8 ASCII
public StudentContainer() {
super("STUDENT DATABASE"); //container header is "ASCII PROGRAM"
newGUI(); //calls newGUI to initialize GUI
readFile();
} // end constructor{
private void newGUI(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //exit on close statement
Container p = this.getContentPane(); //declare container p
p.setLayout(new BorderLayout()); //set p to BorderLayout
Panel pCenter = new Panel(); //creates pCenter panel
pCenter.setLayout(new GridLayout(0,2)); //initiates grid layout
pCenter.add(lblFirstName); //adds Name label to pCenter
pCenter.add(FirstNameInput); //adds Phone text field to pCenter
pCenter.add(lblLastName); //adds Name label to pCenter
pCenter.add(LastNameInput); //adds Phone text field to pCenter
pCenter.add(lblAddressLine1); //adds Name text field to pCenter
pCenter.add(AddressLine1Input); //adds Phone text field to pCenter
pCenter.add(lblAddressLine2); //adds Name label to pCenter
pCenter.add(AddressLine2Input); //adds Phone text field to pCenter
pCenter.add(lblCity); //adds Name label to pCenter
pCenter.add(CityInput); //adds Phone text field to pCenter
pCenter.add(lblState); //adds Name label to pCenter
pCenter.add(StateInput); //adds Phone text field to pCenter
pCenter.add(lblZip); //adds Name label to pCenter
pCenter.add(ZipInput); //adds Phone text field to pCenter
pCenter.add(lblBirthday); //adds Name label to pCenter
pCenter.add(BirthdayInput); //adds Phone text field to pCenter
pCenter.add(lblGradDate); //adds Name label to pCenter
pCenter.add(GradDateInput); //adds Phone text field to pCenter
pCenter.add(lblGpa); //adds Name label to pCenter
pCenter.add(GpaInput); //adds Phone text field to pCenter
pCenter.add(lblCreditHours); //adds Name label to pCenter
pCenter.add(CreditHoursInput); //adds Phone text field to pCenter
Panel pSouth = new Panel(); //creates pSouth panel
pSouth.add(clickPrevious); //adds previous button
pSouth.add(clickSave); //adds save button
pSouth.add(clickDelete); //adds delete button
pSouth.add(clickNext); //adds next button
p.add(pCenter, BorderLayout.CENTER); //adds pCenter panel to container
p.add(pSouth, BorderLayout.SOUTH); //adds pSouth panel to container
clickPrevious.addActionListener(this); //assigns ActionListener to previous button
this.pack();
this.setVisible(true);
clickSave.addActionListener(this); //assigns ActionListener to save button
this.pack();
this.setVisible(true);
clickDelete.addActionListener(this); //assigns ActionListener to delete button
this.pack();
this.setVisible(true);
clickNext.addActionListener(this); //assigns ActionListener to next button
this.pack();
// ReadFile(); //calls ReadFile to read the AddressBook.txt file into the ArrayLists
// if (Name.size() != 0) //if there is no empty data
// counter = 0; //set the counter to the first record
// else
// counter = -1; //set the counter to -1 if there is no data in the text file
//// updateScreen(); //calls update screen to show blank text fields
// this.setVisible(true);
}//end newGUI
public void actionPerformed(ActionEvent e) {
// if (e.getSource() == clickSave){ //if save button is clicked, save unless there is nothing to save
// String x = NameInput.getText();
// String y = AddressInput.getText();
// String z = PhoneInput.getText();
//
// if(x.equals("")){ //if name is blank
// JOptionPane.showMessageDialog(null, "Name must be entered!"); //display error message
// }//end else if
// else if(y.equals("")){ //if address is blank
// JOptionPane.showMessageDialog(null, "Address must be entered!"); //display error message
// }//end else if
// else if(z.equals("")){ //if phone number is blank
// JOptionPane.showMessageDialog(null, "Phone Number must be entered!"); //display error message
// }//end else if
// else{ //if all items are entered
// Name.add(NameInput.getText()); //updates list with Name
// Address.add(AddressInput.getText()); //updates list with Address
// Phone.add(PhoneInput.getText()); //updates list with Phone
// WriteFile(); //calls WriteFile to save to the text file
// counter = Name.size() - 1; //takes counter to last record of list
// JOptionPane.showMessageDialog(null, "Save is complete!"); //display message to confirm save to user
// }//end else
//
// }//end if
//
// else if (e.getSource() == clickDelete){ //if delete is clicked, delete unless there is nothing to delete
// if(counter >= 0){ //if the counter is greater than 0
// Name.remove(counter); //remove name from name list
// Address.remove(counter); //remove address from address list
// Phone.remove(counter); //remove phone number from phone list
// WriteFile(); //deletes record from text file
// if (counter == Name.size()){ //if counter was last record
// if (Name.size() == 0) //if size is empty then counter becomes -1
// counter = -1;
// else
// counter = counter - 1; //sets counter to previous record
// }
// updateScreen(); //call update method to display blank text fields
// JOptionPane.showMessageDialog(null, "Entry has been Deleted!"); //display message to confirm save to user
// }//end if
// else {
// BlankGUI(); //if counter is not greater than 0
// JOptionPane.showMessageDialog(null, "There are no records!"); //display error message to user
// }//end else
// }//end else if
//
// else if (e.getSource() == clickPrevious){ //if previous is clicked, go to previous unless there is no previous
// if(counter > 0){ //if counter is greater than 0
// counter--;
// updateScreen(); //call update method to display blank form
// }
// else
// JOptionPane.showMessageDialog(null, "This is the first record"); //display message to user
// }//end else if
//
// else if (e.getSource() == clickNext){ //if next is clicked, go to next unless there is nothing next.
// if(counter > -1){ //if the arrayList is not empty
//
// if (counter<Name.size() - 1){ //if we are not at the end of the ArrayList, go to next element
// counter++; //go to next record
// updateScreen();
// }//end if
// else JOptionPane.showMessageDialog(null, "This is the last record!"); //else display message to let user know we are at the end
//
// }//end if
// else JOptionPane.showMessageDialog(null, "There are no records!"); //display message to let user know there are no records
//
// }//end else if
//
// else JOptionPane.showMessageDialog(null, "Error!");
}
public void readFile(){
BufferedReader readBuffer;
try {
readBuffer = new BufferedReader(new FileReader("Students.txt"));
String strRead;
int p = 0;
while((strRead = readBuffer.readLine())!=null){
System.out.println("readFile first step in while");
String line1 = strRead;
System.out.println(line1);
System.out.println("readFile first step in while2");
students[p].parse(line1); //<==Null Pointer??
System.out.println("readFile after first parse call");
System.out.println(line[p]);
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}