I am trying to make an address book that stores the normal stuff. I have everything done except I can't get the buttons to work, ie, Add, Delete, Search, View. If you can take some time and look over my code and help me out it would be greatly appreciated...Thanks
publicclass AddressBook
{
// Use these class variables to store values for searching
private Person person[] = new Person[50];
private String guiLastName;
private String guiFirstName;
privateint curpos;
public AddressBook()
{
buildGui();
} // end AddressBook constructor
public TextField firstName,
lastName,
phone,
email,
city,
state,
zip;
public Component buildGui()
{
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");
final JButton add = new JButton(new AddAction());
final JButton delete = new JButton(new DeleteAction());
final JButton search = new JButton(new SearchAction());
final JButton view = new JButton("View Address Book");
firstName = new TextField(15);
lastName = new TextField(15);
phone = new TextField(15);
email = new TextField(15);
city = new TextField(15);
state = new TextField(15);
zip = new TextField(15);
JPanel pane1 = new JPanel();
pane1.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)
);
pane1.setLayout(new GridLayout(10, 2, 10, 10));
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);
return pane1;
// put addressbook gui componenets in here
// create frame/panels etc..
// example
} // end buildGui
publicstaticvoid main(String args[])
{
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);
//Finish setting up the frame, and show it.
frame.addWindowListener(new WindowAdapter()
{
publicvoid windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
} // end main method
privateint search(String s)
{
//JOptionPane.showMessageDialog(null, "Seach Was Clicked!");
int i= 0;
//put search array here - return the value of index of found item or null address
while (!person[i].getLastName().equals(guiLastName) && person[i] != null)
{
i++;
}
return i;
}
privateclass SearchAction extends AbstractAction
{
public SearchAction()
{
putValue(NAME, "Search");
putValue(SHORT_DESCRIPTION, "Search");
}
publicvoid actionPerformed(ActionEvent e)
{
int i = 0;
long z = 0;
curpos = search(guiLastName);
while (!person[i].getLastName().equals(guiLastName) && person[i] != null)
{
i++;
}
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());
z = person[i].getZip();
zip.setText(Long.toString(z));
// put code for searching here using curpos as index position
}
}
privateclass AddAction extends AbstractAction
{
public AddAction()
{
putValue(NAME, "Add");
putValue(SHORT_DESCRIPTION, "Add");
}
publicvoid actionPerformed(ActionEvent e)
{
//JOptionPane.showMessageDialog(null, "Add Was Clicked!");
curpos = search(guiLastName);
person[curpos] = new Person();
// put code adding here
}
}
privateclass DeleteAction extends AbstractAction
{
public DeleteAction()
{
putValue(NAME, "Delete");
putValue(SHORT_DESCRIPTION, "Delete");
}
publicvoid actionPerformed(ActionEvent e)
{
//JOptionPane.showMessageDialog(null, "Delete Was Clicked!");
curpos = search(guiLastName);
person[curpos] = null;
// put code for deleting here
}
}
} // end AddressBook class
class Person
{
private String firstName;
private String lastName;
private String email;
private String city;
private String state;
private String phoneNumber;
privatelong zip;
private String firstNam;
private String lastNam;
private String mail;
private String city1;
private String stat;
private String phoneNumb;
privatelong zip1;
//My constructor for the person class
public Person(String a, String b, String c,
String e, String f, String g, long h)
{
setFirstName(a);
setLastName(b);
setEmail(c);
setCity(e);
setState(f);
setPhone(g);
setZip(h);
}
public Person()
{
}
//These are my methods to try to get and send the text you enter
public String getFirstName()
{
return firstNam;
}
publicvoid setFirstName(String firstNam)
{
firstName = firstNam;
}
public String getLastName()
{
return lastNam;
}
publicvoid setLastName(String lastNam)
{
lastName = lastNam;
}
public String getEmail()
{
return mail;
}
publicvoid setEmail(String mail)
{
email = mail;
}
public String getCity()
{
return city1;
}
publicvoid setCity(String city1)
{
city = city1;
}
public String getState()
{
return stat;
}
publicvoid setState(String stat)
{
state = stat;
}
public String getPhone()
{
return phoneNumb;
}
publicvoid setPhone(String phoneNumb)
{
phoneNumber = phoneNumb;
}
publiclong getZip()
{
return zip1;
}
publicvoid setZip(long zip1)
{
zip = zip1;
}
}
Well, I said that the program will work. I did not say that it will execute correctly. I downloaded your source code and found out that there are too many mistakes in your program.
for eg.
in the actionPerformed method of AddAction, you are doing a search :
curpos = search(lastName.getText());
Your search method throws NullPointerException all the time, and there are plenty of other issues. It will be better if you reorganize your code and have a clear objective in your mind as to what you want to achieve.