import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
class AddRecord extends JDialog implements Serializable
{
/**
 * 
 */

private class backActionListener implements ActionListener 
{ public void actionPerformed(ActionEvent arg0)
{

setVisible(false);

}

}
private class addActionListener implements ActionListener 
{
public void actionPerformed(ActionEvent arg0) 
{
try
{
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("phoneBook.dat"));
phoneList=new LinkedList<Person>();
phoneList.add(new Person(displayName.getText(),displayPhone.getText(),displaySex.getText()));
ListIterator<Person> iter=phoneList.listIterator();
while(iter.hasNext())
{
out.writeObject(iter.next());
}
}
catch(Exception e)
{
e.printStackTrace();
}
}

}
private  JTextField displayName;
private  JTextField displayPhone;
private  JTextField displaySex;
private JPanel panel;
private LinkedList<Person> phoneList;
public AddRecord(JDialog dialog)
{
super(dialog,"AddRecord",true);

this.setSize(dialog.getWidth(), dialog.getHeight());
this.setLocation(dialog.getWidth(),dialog.getHeight());

panel=new JPanel();
panel.setLayout(new GridLayout(3,3,5,5));

addText("Name:",displayName);
addText("Phone:",displayPhone);
addText("Sex:",displaySex);
add(panel,BorderLayout.CENTER);

JPanel button=new JPanel();
JButton add=new JButton("add");
JButton back=new JButton("back");
button.add(add);
button.add(back);
add(button,BorderLayout.SOUTH);
ActionListener addAction=new addActionListener();
add.addActionListener(addAction);
ActionListener backAction=new backActionListener();
back.addActionListener( backAction);

}
private void addText(String s1,JTextField text)
{
Label l0=new Label(s1);
panel.add(l0);
text=new JTextField();
panel.add(text);
}
}
当我点击按钮"add"后就出现这样的问题:java.lang.NullPointerException
at AddRecord$addActionListener.actionPerformed(AddRecord.java:37)
请高手指点下哦。。

解决方案 »

  1.   

    private class addActionListener implements ActionListener 

    public void actionPerformed(ActionEvent arg0) 

    try 

    ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("phoneBook.dat")); //这边是对象流
    phoneList=new LinkedList <Person>(); //一个Person对象的链表
    phoneList.add(new Person(displayName.getText(),displayPhone.getText(),displaySex.getText())); 
    ListIterator <Person> iter=phoneList.listIterator(); //一个迭代器
    while(iter.hasNext()) 

    out.writeObject(iter.next()); 


    catch(Exception e) 

    e.printStackTrace(); 


    应该是这里出问题了,但不知道对象流是否怎么跟链表相结合?高手能指点下么?
    Person是个对象:
    class Person
    {
    private String name;
    private String phone;
    private String sex;}
      

  2.   

    呵呵,一不注意很容易犯的错误注意看你的displayName,他是class AddRecord 的属性而你displayName.getText(),是在你的private class addActionListener类里的
    actionPerformed(ActionEvent arg0) 面actionPerformed(ActionEvent arg0) 
    只有一个参数
    那你的displayName.getText()的displayName哪里来的?不要以为addActionListener是AddRecord的内部类,就能直接使用AddRecord的变量两种办法,
    1)在addActionListener中添加属性:displayName或者AddRecord本身,构造时传进去
    2)通过参数ActionEvent.getSource()或者什么的方法可以取到产生事件的元素,也就是AddRecord本身
      

  3.   

    你好 我是新手 请问下 我把你的程序放在netbeans中调试 怎么说下面这个语句有问题的啊?
    phoneList=new LinkedList <Person>(); //一个Person对象的链表 LZ 回答下OK?
     
      

  4.   

    phoneList=new LinkedList <Person>();漏看了,跟我上面说的displayName一样不要以为addActionListener是AddRecord的内部类,就能直接使用AddRecord的变量 
      

  5.   

    按你的方法试了一下现在是这个问题:java.io.EOFException
    应该是序列化的问题我用的是对象流
      

  6.   

    因为我给的代码省略了一些,
    看我上面补充的代码就知道了- -
    Person是个对象- -