I am creating an address book and I cant get the input/output to work correctly. I know it is saving things to the file but it isnt working correctly because it is saving the errors that im getting instead of the people in my address book. If someone could take a look at the following code and give me a little help it would be greatly appreciated.
// Import Statements
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javabook.*;
import java.awt.event.ActionEvent;
publicclass AddressBook
{
// Use these class variables to store values for searching
// Person Array Declared
privatestatic Person person[] = new Person[50];
// Declared TextFields
JTextField lastName;
JTextField firstName;
JTextField phone;
JTextField email;
JTextField city;
JTextField state;
JTextField zip;
// Counter Declared
privateint i;
// AddressBook Constructor Use To Build The GUI Interface
public AddressBook()
{
buildGui();
} // End AddressBook constructor
// GUI Interface
public Component buildGui()
{
// GUI Lables
final JLabel firstLabel = new JLabel("First Name");
final JLabel lastLabel = new JLabel("Last Name");
final JLabel phoneLabel = new JLabel("Phone Number");
final JLabel emailLabel = new JLabel("E-mail Address");
final JLabel cityLabel = new JLabel("City");
final JLabel stateLabel = new JLabel("State");
final JLabel zipLabel = new JLabel("Zip");
// GUI Buttons
final JButton add = new JButton(new AddAction());
final JButton delete = new JButton(new DeleteAction());
final JButton search = new JButton(new SearchAction());
final JButton save = new JButton(new SaveAction());
final JButton open = new JButton(new OpenAction());
final JButton view = new JButton("View Address Book");
// GUI TextFields
firstName = new JTextField(15);
lastName = new JTextField(15);
phone = new JTextField(15);
email = new JTextField(15);
city = new JTextField(15);
state = new JTextField(15);
zip = new JTextField(15);
// GUI Pannel
JPanel pane1 = new JPanel();
// Set GUI Panel Size
pane1.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
pane1.setLayout(new GridLayout(10, 2, 10, 10));
// Adds GUI Componets To The GUI Panel;
pane1.add(firstLabel);
pane1.add(firstName);
pane1.add(lastLabel);
pane1.add(lastName);
pane1.add(phoneLabel);
pane1.add(phone);
pane1.add(emailLabel);
pane1.add(email);
pane1.add(cityLabel);
pane1.add(city);
pane1.add(stateLabel);
pane1.add(state);
pane1.add(zipLabel);
pane1.add(zip);
pane1.add(add);
pane1.add(delete);
pane1.add(search);
pane1.add(view);
pane1.add(save);
pane1.add(open);
// Return The Panel To Be Displayed
return pane1;
} // End buildGui
publicstaticvoid main(String args[])
{
// GUI Look And Feel
try
{
UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName());
}
catch (Exception e)
{
}
//Create the top-level container and add contents to it.
JFrame frame = new JFrame("AddressBook");
AddressBook app = new AddressBook();
Component contents = app.buildGui();
frame.getContentPane().add(contents, BorderLayout.CENTER);
frame.addWindowListener(new WindowAdapter()
{
publicvoid windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}// End Main Method
privateint search(String s)
{
int i= 0;
while (person[i] != null && !person[i].getLastName().equals(s))
{
i++;
}
return i;
}
// Search Method
privateclass SearchAction extends AbstractAction
{
public SearchAction()
{
putValue(NAME, "Search");
putValue(SHORT_DESCRIPTION, "Search");
}
publicvoid actionPerformed(ActionEvent e)
{
i = search(lastName.getText());
if(person[i] == null)
{
JOptionPane.showMessageDialog(null, lastName.getText() + " Person Not Found");
lastName.setText(" ");
}
else
{
firstName.setText(person[i].getFirstName());
lastName.setText(person[i].getLastName());
phone.setText(person[i].getPhone());
email.setText(person[i].getEmail());
city.setText(person[i].getCity());
state.setText(person[i].getState());
phone.setText(person[i].getPhone());
zip.setText(person[i].getZip());
}
}
}
// Add Method
privateclass AddAction extends AbstractAction
{
public AddAction()
{
putValue(NAME, "Add");
putValue(SHORT_DESCRIPTION, "Add");
}
publicvoid actionPerformed(ActionEvent e)
{
int i = 0;
while (person[i] != null)
i++;
if (person[i] == null)
{
person[i] = new Person(firstName.getText(),lastName.getText(),phone.getText(),
email.getText(),city.getText(),state.getText(),zip.getText());
firstName.setText(" ");
lastName.setText(" ");
phone.setText(" ");
email.setText(" ");
city.setText(" ");
state.setText(" ");
zip.setText(" ");
JOptionPane.showMessageDialog(null, person[i].getFirstName() + " " + person[i].getLastName() + " Was Added!");
i++;
}
}
}
// Delete Method
privateclass DeleteAction extends AbstractAction
{
public DeleteAction()
{
putValue(NAME, "Delete");
putValue(SHORT_DESCRIPTION, "Delete");
}
publicvoid actionPerformed(ActionEvent e)
{
int i = 0;
i = search(lastName.getText());
while (!person[i].getLastName().equals(lastName) && person[i] != null)
i++;
if(person[i].getLastName().equals(lastName))
{
firstName.setText(" ");
lastName.setText(" ");
phone.setText(" ");
email.setText(" ");
city.setText(" ");
state.setText(" ");
zip.setText(" ");
person[i].setFirstName(null);
person[i].setLastName(null);
person[i].setEmail(null);
person[i].setCity(null);
person[i].setState(null);
person[i].setPhone(null);
person[i].setZip(null);
}
}
}
// Save Method
privateclass SaveAction extends AbstractAction
{
public SaveAction()
{
putValue(NAME, "Save");
putValue(SHORT_DESCRIPTION, "Save");
}
publicvoid actionPerformed(ActionEvent e)throws IOException, ClassNotFoundException
{
MainWindow mainWindow;
mainWindow = new MainWindow("");
FileDialog fileBox = new FileDialog(mainWindow, "Save", FileDialog.SAVE);
fileBox.setVisible(true);
File outFile = new File("person.dat");
FileOutputStream outFileStream = new FileOutputStream(outFile);
ObjectOutputStream outObjectStream = new ObjectOutputStream(outFileStream);
outObjectStream.writeObject(person);
outObjectStream.close();
}
}
// Open Method
privateclass OpenAction extends AbstractAction
{
public OpenAction()
{
putValue(NAME, "Open");
putValue(SHORT_DESCRIPTION, "Open");
}
publicvoid actionPerformed(ActionEvent e)throws IOException, ClassNotFoundException
{
MainWindow mainWindow;
mainWindow = new MainWindow("");
FileDialog fileBox = new FileDialog(mainWindow, "Open", FileDialog.LOAD);
fileBox.setVisible(true);
File inFile = new File("person.dat");
FileInputStream inFileStream = new FileInputStream(inFile);
ObjectInputStream inObjectStream = new ObjectInputStream(inFileStream);
inObjectStream.readObject();
inObjectStream.close();
}
}
} // End AddressBook class
// Person Method
class Person
{
// Private Variables
private String firstName;
private String lastName;
private String email;
private String city;
private String state;
private String phoneNumber;
private String zip;
// Person Constructor
public Person()
{
}
// My Constructor For The Person Class
public Person(String a, String b, String c,
String e, String f, String g, String h)
{
setFirstName(a);
setLastName(b);
setEmail(c);
setCity(e);
setState(f);
setPhone(g);
setZip(h);
}
// Set And Get Methods
public String getFirstName()
{
return firstName;
}
publicvoid setFirstName(String firstNam)
{
firstName = firstNam;
}
public String getLastName()
{
return lastName;
}
publicvoid setLastName(String lastNam)
{
lastName = lastNam;
}
public String getEmail()
{
return email;
}
publicvoid setEmail(String mail)
{
email = mail;
}
public String getCity()
{
return city;
}
publicvoid setCity(String city1)
{
city = city1;
}
public String getState()
{
return state;
}
publicvoid setState(String stat)
{
state = stat;
}
public String getPhone()
{
return phoneNumber;
}
publicvoid setPhone(String phoneNumb)
{
phoneNumber = phoneNumb;
}
public String getZip()
{
return zip;
}
publicvoid setZip(String zip1)
{
zip = zip1;
}
}