2 tesjunma tesjunma 于 2017.01.05 21:14 提问

java swing 登陆界面 空指针异常

我这里是一个简单的用户登录的程序,有两个Jpanel(pnlTop和主体部分),主体部分有user的JPanel,PassWord的JPanel。
其中,用户的密码、用户名信息都是存储在"information"路径下的"Customer.txt"文件里面的。
isCorrect()和checkInput()属于自定义的方法,用于检查输入和文件内的信息是否匹配。

 package UI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import Controller.CustomerController;
import Customer.Customer;
import myComp.MyPicPanel;

public class LoginForm extends BaseDialog{

    private JPanel pnlMain = null;
    private JPanel pnlTop = null;

    private JPanel pnlUsername = null;
    private JPanel pnlPassword =null;
    private JPanel pnlButton = null;

    private JLabel lblUsername = null;
    private JLabel lblPassword= null;

    private JButton btnExit= null;
    private JButton btnLogin= null;

    private JTextField txtUsername= null;
    private JPasswordField txtPassword= null;

    public LoginForm(){


        pnlTop = new MyPicPanel("images/2.jpg",400,90);
        pnlTop.setPreferredSize(new Dimension(400,90));

        this.add(pnlTop, BorderLayout.NORTH);


        pnlMain = new JPanel();
        pnlMain.setLayout(new BorderLayout());
        pnlMain.setBorder(new EmptyBorder(5,0,0,0));
        pnlMain.setBackground(Color.LIGHT_GRAY);
        this.add(pnlMain, BorderLayout.CENTER);

        initUI();

    }

    public void initUI(){


        //user name panel
        pnlUsername = new JPanel();
        pnlUsername.setPreferredSize(new Dimension(this.getWidth(),60));
        pnlUsername.setBorder(new EmptyBorder(10,0,0,0));
        pnlMain.add(pnlUsername,BorderLayout.NORTH);

        //password panel
        pnlPassword = new JPanel();
        pnlPassword.setPreferredSize(new Dimension(this.getWidth(),60));
        pnlMain.add(pnlPassword,BorderLayout.CENTER);

        //button panel
        pnlButton = new JPanel();
        pnlButton.setPreferredSize(new Dimension(this.getWidth(),60));
        pnlMain.add(pnlButton,BorderLayout.SOUTH);

        addUsername(pnlUsername);
        addPassword(pnlPassword);
        addButton(pnlButton);
    }



     public ActionListener onclickListener = new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e){
            JButton jb = (JButton)e.getSource();
            if(jb.getActionCommand().equals("login")){
                if(checkInput()){
                        if(isCorrect()){
                            Mainform main = new Mainform();
                            dispose();
                        }
                    }else{
                    JOptionPane.showMessageDialog(null, "登录失败","Gourmet Coffee System",
                            JOptionPane.ERROR_MESSAGE);
                }
            }else if(jb.getActionCommand().equals("exit")){
                System.exit(0);
            }
        }

    };




    private void addPassword(JPanel pnlPassword) {

        JLabel lblPassword = new JLabel("Password");
        lblPassword.setFont(new Font("Microsoft YaHei UI",Font.BOLD,20));
        pnlPassword.add(lblPassword);
        txtPassword = new JPasswordField(10); 
        pnlPassword.add(txtPassword);
    }


    private void addUsername(JPanel pnlUsername) {

        JLabel lblUsername = new JLabel("Account");
        lblUsername.setFont(new Font("Microsoft YaHei UI",Font.BOLD,20));
        pnlUsername.add(lblUsername);
        txtUsername = new JTextField(10);
        pnlUsername.add(txtUsername);

    }

    //默认FlowLayout
        private void addButton(JPanel pnlButton) {
            btnLogin = new JButton("Login");
            btnLogin.setActionCommand("login");
            btnLogin.addActionListener(onclickListener);
            pnlButton.add(btnLogin);

            btnExit = new JButton("Exit");
            btnExit.setActionCommand("exit");
            btnExit.addActionListener(onclickListener);
            pnlButton.add(btnExit);

        }
    public boolean isCorrect(){
        boolean flag = true;
        CustomerController userManager =  new CustomerController();
        ArrayList<Customer> customers = userManager.readObject("information","Customer.txt");

        for(Customer customer: customers){
            if(customer.getAccount().equals(txtUsername.getText().trim()))
            {
                if((String.valueOf(txtPassword.getPassword().equals(customer.getPassword()))) != null){
                    flag = true;
                }
            }
        }

        return flag;
    }

    public boolean checkInput(){
        boolean flag = true;
        if(txtUsername.getText().trim().isEmpty()){
            this.showMessage("账号不许为空!");
            txtUsername.requestFocus();
            flag = false;
        }

        if(String.valueOf(txtPassword.getPassword()).trim().isEmpty()){
            this.showMessage("密码不许为空!");
            txtPassword.requestFocus();
            flag = false;
        }

        return flag;
    }

    public void showMessage(String msg){
        JOptionPane.showMessageDialog(null, msg,this.getTitle(),JOptionPane.ERROR_MESSAGE);
    }
}

输出界面是这样:
图片说明
但是每次输入用户名和密码(只要不是空,无论输入什么)都会报空指针异常。

这是异常片段:

 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at UI.Mainform.removeButton(Mainform.java:134)
    at UI.Mainform.<init>(Mainform.java:71)
    at UI.LoginForm$1.actionPerformed(LoginForm.java:95)

折磨了我整整一个下午加晚上…明天就要交了 求救啊各位大佬……

1个回答

yushiyoufight
yushiyoufight   Rxr 2017.01.06 11:01

at UI.Mainform.removeButton(Mainform.java:134)
这不提示了么在MainForm 134行报的错,打个断点,debug跟一下,就查到了

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!