|
Null Pointer Errors!
I followed the books code, the code will compile, but it gets null pointer errors when i type "java cm" please help. Here is the code:
JAVA Code:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
class cm extends Frame implements ActionListener
{
private Vector contacts = new Vector(100);
private java.awt.List names = new java.awt.List();
private Button delete;
private Button update;
public cm()
{
super("Contact Manager");
addWindowListener(new WindowAdapter ()
{
public void windowClosing
(WindowEvent e)
{
saveContacts();
System.exit(0);
}
});
Label l = new Label();
add("North", l);
add("Center", names);
Panel p = new Panel();
Button b;
p.add(b = new Button("Insert"));
b.addActionListener(this);
p.add(b = new Button("Delete"));
delete.addActionListener(this);
delete.setEnabled(false);
p.add(update = new Button("Update"));
update.addActionListener(this);
update.setEnabled(false);
p.add(b = new Button("Finish"));
b.addActionListener(this);
add("South", p);
setBackground(Color.lightGray);
setSize(400, 200);
setResizable(false);
loadContacts();
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Delete"))
delete();
else
if(e.getActionCommand().equals("Finish"))
{
saveContacts();
System.exit(0);
}
else
if(e.getActionCommand().equals("Insert"))
insert();
else
update();
}
public Insets getInsets()
{
return new Insets(10, 10, 10, 10);
}
public static void main(String [] args)
{
new cm();
}
private void delete()
{
int index = names.getSelectedIndex();
if(index != -1)
{
names.remove(index);
contacts.remove(index);
if(contacts.size() == 0)
{
delete.setEnabled(true);
update.setEnabled(true);
}
else
names.select(0);
}
}
private void insert()
{
DataEntryForm def = new DataEntryForm(this, "Insert");
if(def.bOk)
{
Contact temp = new Contact();
temp.fname = new String(def.fname.getText());
temp.lname = new String(def.lname.getText());
temp.phone = new String(def.phone.getText());
temp.fax = new String(def.fax.getText());
temp.email = new String(def.phone.getText());
names.add(temp.lname + ", " + temp.fname);
contacts.add(temp);
delete.setEnabled(true);
update.setEnabled(true);
}
def.dispose();
names.select(0);
}
private void loadContacts()
{
FileInputStream fis = null;
try
{
fis = new FileInputStream("contacts.dat");
DataInputStream dis = new DataInputStream(fis);
int nContacts = dis.readInt();
for(int i = 0; i < nContacts; i++)
{
Contact temp = new Contact();
temp.fname = dis.readUTF();
temp.lname = dis.readUTF();
temp.phone = dis.readUTF();
temp.fax = dis.readUTF();
temp.email = dis.readUTF();
names.add(temp.lname + ", " + temp.fname);
contacts.add(temp);
}
if(nContacts > 0)
{
delete.setEnabled(true);
update.setEnabled(true);
}
}
catch(IOException e)
{
System.out.println("Error!");
}
finally
{
try
{
fis.close();
}
catch(IOException e){
System.out.println("Error");
}
}
names.select(0);
}
private void saveContacts()
{
FileOutputStream fos = null;
try
{
fos = new FileOutputStream("contacts.dat");
DataOutputStream dos = new DataOutputStream(fos);
dos.writeInt(contacts.size());
for(int i = 0; i < contacts.size(); i++)
{
Contact temp = (Contact) contacts.elementAt(i);
dos.writeUTF(temp.fname);
dos.writeUTF(temp.lname);
dos.writeUTF(temp.phone);
dos.writeUTF(temp.fax);
dos.writeUTF(temp.email);
}
}
catch(IOException e)
{
MsgBox mb = new MsgBox(this, "CM Error",
e.toString());
mb.dispose();
}
finally
{
try
{
fos.close();
}
catch(IOException e){System.out.println("Error!");
}
}
}
private void update()
{
int index = names.getSelectedIndex();
if(index != -1)
{
Contact temp = (Contact) contacts.elementAt(index);
DataEntryForm def = new DataEntryForm (this, "Update",
temp.fname,
temp.lname,
temp.phone,
temp.fax,
temp.email);
if(def.bOk)
{
temp.fname = new String(def.fname.getText());
temp.lname = new String(def.lname.getText());
temp.phone = new String(def.phone.getText());
temp.fax = new String(def.fax.getText());
temp.email = new String(def.email.getText());
names.replaceItem(temp.lname + ", " + temp.fname,
index);
}
def.dispose();
names.select(0);
}
}
}
class Contact
{
public String fname;
public String lname;
public String phone;
public String fax;
public String email;
}
class DataEntryForm extends Dialog implements ActionListener
{
public boolean bOk;
public TextField fname;
public TextField lname;
public TextField phone;
public TextField fax;
public TextField email;
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Ok"))
bOk = true;
dispose();
}
public DataEntryForm(Frame parent, String title)
{
this(parent, title, "","","","","");
}
public DataEntryForm (Frame parent, String title, String fname, String lname, String phone, String fax, String email)
{
super(parent, title, true);
setLayout(new GridBagLayout());
addComponent(this, new Label("First name: "), 0, 0, 1, 1,
GridBagConstraints.NONE,
GridBagConstraints.WEST);
this.fname = new TextField(15);
addComponent(this, this.fname, 1, 0, 1, 1,
GridBagConstraints.NONE,
GridBagConstraints.CENTER);
if(title.equals("Update"))
this.fname.setText(fname);
addComponent(this, new Label("Last name: "), 0, 1, 1, 1,
GridBagConstraints.NONE,
GridBagConstraints.WEST);
this.lname = new TextField(15);
addComponent(this, this.lname, 1, 1, 1, 1,
GridBagConstraints.NONE,
GridBagConstraints.CENTER);
if(title.equals("Update"))
this.lname.setText(lname);
addComponent(this, new Label("Phone number: "), 0, 2, 1, 1,
GridBagConstraints.NONE,
GridBagConstraints.WEST);
this.phone = new TextField(15);
addComponent(this, this.phone, 1, 2, 1, 1,
GridBagConstraints.NONE,
GridBagConstraints.CENTER);
if(title.equals("Update"))
this.phone.setText(phone);
addComponent(this, new Label("FAX number: "), 0, 3, 1, 1,
GridBagConstraints.NONE,
GridBagConstraints.WEST);
this.fax = new TextField(15);
addComponent(this, this.fax, 1, 3, 1, 1,
GridBagConstraints.NONE,
GridBagConstraints.CENTER);
if(title.equals("Update"))
this.fax.setText(fax);
addComponent(this, new Label("Email Address: "), 0, 4, 1, 1,
GridBagConstraints.NONE,
GridBagConstraints.WEST);
this.email = new TextField(25);
addComponent(this, this.email, 1, 4, 1, 1,
GridBagConstraints.NONE,
GridBagConstraints.CENTER);
if(title.equals("Update"))
this.email.setText(email);
addComponent(this, new Label(""), 0, 5, 1, 1,
GridBagConstraints.NONE,
GridBagConstraints.CENTER);
addComponent(this, new Label(""), 1, 5, 1, 1,
GridBagConstraints.NONE,
GridBagConstraints.CENTER);
Button b;
addComponent(this, b = new Button ("Ok"), 0, 6, 1, 1,
GridBagConstraints.NONE,
GridBagConstraints.CENTER);
b.addActionListener(this);
addComponent(this, b = new Button("Cancel"), 1, 6, 1, 1,
GridBagConstraints.NONE,
GridBagConstraints.CENTER);
b.addActionListener(this);
setSize(250, 200);
setResizable(false);
setVisible(true);
}
private void addComponent(Container con, Component com, int gridx, int gridy, int gridw, int gridh, int fill, int anchor){
LayoutManager lm = con.getLayout();
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridw;
gbc.gridheight = gridh;
gbc.fill = fill;
gbc.anchor = anchor;
((GridBagLayout) lm).setConstraints(com, gbc);
con.add(com);
}
}
class MsgBox extends Dialog implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
dispose();
}
public MsgBox(Frame parent, String title, String msg)
{
super(parent, title, true);
Label l = new Label(msg);
add("Center", l);
Button b = new Button("Ok");
add("South", b);
b.addActionListener(this);
b.requestFocus();
setResizable(false);
pack();
setVisible(true);
}
}
Last edited by admin : 11-Oct-2005 at 21:12.
Reason: Please insert your JAVA code between [java] & [/java] tags
|