首先,我创建一个JFrame, 有JTextField和JPasswordField,两个JButton分别是注册和登录
我想实现的是,填入账户和密码, 点击注册,然后把生成的user已Object保存在txt文件里面
然后再运行程序,输入账户和密码,点击登录,这时从txt文件里面取出Object和输入的账号
和密码作比较,然后在控制台打印成功或者失败!下面发代码, 虽然知道会报空指针错误,但是不知道怎么修改,求帮助! 谢谢了!

解决方案 »

  1.   

    main测试类import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class MainFrame extends JFrame implements ActionListener { private JPasswordField passwordField;
    private JTextField textField;
    private JButton regButton;
    private JButton loginButton;
    private User user = new User(); public static void main(String args[]) {
    new MainFrame();
    } public MainFrame() {
    init();

    }

    public void init() {
    setTitle("注册测试");
    getContentPane().setLayout(null);
    setBounds(450, 150, 364, 233);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); textField = new JTextField();
    textField.setBounds(99, 25, 171, 22);
    getContentPane().add(textField); passwordField = new JPasswordField();
    passwordField.setBounds(99, 72, 171, 22);
    getContentPane().add(passwordField); final JLabel label = new JLabel();
    label.setText("账号");
    label.setBounds(53, 27, 40, 18);
    getContentPane().add(label); final JLabel label_1 = new JLabel();
    label_1.setText("密码");
    label_1.setBounds(53, 74, 40, 18);
    getContentPane().add(label_1); regButton = new JButton();
    regButton.setText("注册");
    regButton.setBounds(93, 131, 71, 28);
    getContentPane().add(regButton);
    regButton.addActionListener(this); loginButton = new JButton();
    loginButton.setText("登录");
    loginButton.setBounds(201, 131, 60, 28);
    getContentPane().add(loginButton);
    loginButton.addActionListener(this);

    setVisible(true);
    } public void actionPerformed(ActionEvent e) {
    Object obj = e.getSource();
    if(obj == regButton) {
    User user1 = new User();
    String name = this.getTextField().getText();
    String password = String.valueOf(this.getPasswordField().getPassword());
    user1.addUser(name, password);
    System.out.println("注册成功");
    } else if(obj == loginButton) {
    user = user.getUser();
    String pw = String.valueOf(this.getPasswordField().getPassword());
    if(user.getName().equals(this.getTextField().getText()) && 
    user.getPassword().equals(pw)) {
    System.out.println("登录成功");
    } else {
    System.out.println("登录失败");
    }
    }
    } public JPasswordField getPasswordField() {
    return passwordField;
    } public void setPasswordField(JPasswordField passwordField) {
    this.passwordField = passwordField;
    } public JTextField getTextField() {
    return textField;
    } public void setTextField(JTextField textField) {
    this.textField = textField;
    }}
      

  2.   

    这是User类import java.io.*;public class User implements Serializable {

    private String name;
    private String password;

    public User() {

    }

    public User(String name, String password) {
    super();
    this.name = name;
    this.password = password;
    }

    public void addUser(String name, String password) {
    try {
    User user = new User();
    FileOutputStream fos = new FileOutputStream("D:/user.txt");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(user);
    oos.flush();
    oos.close();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    System.out.println("文件不存在!");
    e.printStackTrace();
    }
    } public User getUser() {
    try {
    FileInputStream fis = new FileInputStream("D:/user.txt");
    ObjectInputStream ois = new ObjectInputStream(fis);
    User user = (User)ois.readObject();
    if(user != null) {
    return user;


    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    }
    return this;
    }

    public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } public String getPassword() {
    return password;
    } public void setPassword(String password) {
    this.password = password;
    }
    }
      

  3.   

    调试了几遍, 我把addUser方法改了一下, 测试打印是成功了, 但是不知道是不是真的OK了 ? public void addUser(String name, String password) {
            try {
                this.name = name;
                this.password = password;
                FileOutputStream fos = new FileOutputStream("D:/user.txt");
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                oos.writeObject(user);
                oos.flush();
                oos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                System.out.println("文件不存在!");
                e.printStackTrace();
            }
        }
      

  4.   

                if(user.getName().equals(this.getTextField().getText()) && 
                        user.getPassword().equals(pw)) {==> 
                if(this.getTextField().getText().equals(user.getName()) &&
                        pw.equals(user.getPassword())) {JTextField 的 getText 不会返回 null
      

  5.   

    输出的不是文本文件,把后缀名从 .txt 改成 .dat 或其他
      

  6.   

    #4楼这句   oos.writeObject(user); 应该改成  oos.writeObject(this);
      

  7.   

         User user = new User();改为 
           User user = new User(name , password);
    给分吧
      

  8.   

        在addUser方法里面的增加一个对象到文件 的构造方法
      

  9.   

    修改User类中下面方法。 public void addUser(String name, String password)
        {
            try
            {
                User user = new User(name,password);            FileOutputStream fos = new FileOutputStream("user.txt");
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                oos.writeObject(user);
                oos.flush();
                oos.close();
            }
            catch (FileNotFoundException e)
            {
                e.printStackTrace();
            }
            catch (IOException e)
            {
                System.out.println("文件不存在!");
                e.printStackTrace();
            }
        }
      

  10.   

    还有一个问题, 我现在注册了多个User对象, 追加储存在了文件里面,但是每次读都是读第一次存进去的对象
    如何操作才能对整个文件储存的对象遍历, 然后相比较
      

  11.   

      4楼你的方法 又没new  要不就new一个空的 再set
     
    要不直接new带参数的
      

  12.   

     User user = new User(name,password); 
    FileOutputStream fos = new FileOutputStream("user.txt",true);
    追加的方法  加上true
      

  13.   


    追加我写了, 我现在遇到的问题是: 写入了两个或多个User对象到文件里面, 然后我登陆的时候,只有第一次
    注册的那个对象有效, 如何遍历文件里面所有存入的对象, 然后拿出来比较
      

  14.   


    照你的意思的话, 就是FIFO的方式读取.
    我后来想过用集合的方法, 用list把对象都存进去, 然后点登陆的时候返回一个list,然后遍历list把对象再
    拿出来比较判断账号和密码