即使输入了值,也会在textfield处出现空指针错误

时间:2018-04-27 19:19:20

标签: java

我一直收到一个null异常错误,我似乎无法解决它。我知道当你尝试使用null值但我将值传递给文本字段时,通常会引发空指针错误,所以我很困惑。寻求反馈:

确切的错误代码:(这是我将actionlistner添加到按钮bt28的地方)

  

" AWT-EventQueue的-0"显示java.lang.NullPointerException           在GUI $ 4.actionPerformed(GUI.java:231)......等"

P.S。这个类在主类中调用,这就是为什么不在这里。我已经进口了awt,swing和util。

public class GUI{
CardLayout cl=new CardLayout();
private String cardNum, pin; 

JLabel header1,header2,header3,header4;

JButton bt01= new JButton("");
//bt01.setBounds(100,150,100,40);
JButton bt02= new JButton("");
JButton bt03= new JButton("");
JButton bt04= new JButton("");
JButton bt05= new JButton("");
JButton bt06= new JButton("");
JButton bt07= new JButton("");
JButton bt08= new JButton("Continue");

JButton bt21= new JButton("");
JButton bt22= new JButton("");
JButton bt23= new JButton("");
JButton bt24= new JButton("");
JButton bt25= new JButton("");
JButton bt26= new JButton("");
JButton bt27= new JButton("");
JButton bt28= new JButton("Continue");

JButton bt31= new JButton("");
JButton bt32= new JButton("");
JButton bt33= new JButton("");
JButton bt34= new JButton("");
JButton bt35= new JButton("Balance Query");
JButton bt36= new JButton("Withdraw");
JButton bt37= new JButton("Deposit");
JButton bt38= new JButton("");

JButton bt41= new JButton("A");
JButton bt42= new JButton("B");
JButton bt43= new JButton("C");
JButton bt44= new JButton("D");
JButton bt45= new JButton("E");
JButton bt46= new JButton("");
JButton bt47= new JButton("Business Accounts");
JButton bt48= new JButton("Cancel");

JPanel panelCont=new JPanel();
JPanel GUI1= new JPanel(new BorderLayout());
JPanel GUI2= new JPanel(new BorderLayout());
JPanel GUI3= new JPanel(new BorderLayout());
JPanel GUI4= new JPanel(new BorderLayout());



public GUI(){

    JFrame frame1 = new JFrame ("JLCB Automated Banking Machine v 1.0 ");
    frame1.setPreferredSize (new Dimension(500, 200));
    header1= new JLabel("JLCB Automated Banking Machine");

    JPanel nPanel1= new JPanel();
    nPanel1.add(header1);
    GUI1.add(nPanel1,BorderLayout.NORTH);

    JLabel cardnumLabel =new JLabel("Enter Card Number");
    JTextField txt= new JTextField(15);

    txt.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent cl){
            cardNum=txt.getText();
            txt.setText(null);
        }
    });

    JPanel cPanel1= new JPanel();
    cPanel1.add(cardnumLabel);
    cPanel1.add(txt);
    GUI1.add(cPanel1, BorderLayout.CENTER);

    JPanel wPanel1=new JPanel(new GridLayout(4,0));
    wPanel1.add(bt01);
    wPanel1.add(bt02);
    wPanel1.add(bt03);
    wPanel1.add(bt04);

    JPanel ePanel1=new JPanel(new GridLayout(4,0));
    ePanel1.add(bt05);
    ePanel1.add(bt06);
    ePanel1.add(bt07);
    ePanel1.add(bt08);

    GUI1.add(wPanel1,BorderLayout.WEST);
    GUI1.add(ePanel1,BorderLayout.EAST);

    //................................
    //                               .
    //           GUI2                .
    //................................ 
    JFrame frame2 = new JFrame ("JLCB Automated Banking Machine v 1.0 ");
    frame2.setPreferredSize (new Dimension(500, 200));
    header2= new JLabel("JLCB Automated Banking Machine");


    JPanel nPanel2= new JPanel();
    nPanel2.add(header1);
    GUI2.add(nPanel2,BorderLayout.NORTH);

    JLabel pinLabel =new JLabel("Enter your pin");
    JTextField txt2= new JTextField(15);

    txt2.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent cl){
            pin=txt2.getText();
            txt2.setText(null);
        }
    });

    JPanel cPanel2= new JPanel();
    cPanel2.add(pinLabel);
    cPanel2.add(txt2);
    GUI2.add(cPanel2, BorderLayout.CENTER);

    JPanel wPanel2=new JPanel(new GridLayout(4,0));
    wPanel2.add(bt21);
    wPanel2.add(bt22);
    wPanel2.add(bt23);
    wPanel2.add(bt24);

    JPanel ePanel2=new JPanel(new GridLayout(4,0));
    ePanel2.add(bt25);
    ePanel2.add(bt26);
    ePanel2.add(bt27);
    ePanel2.add(bt28);

    GUI2.add(wPanel2,BorderLayout.WEST);
    GUI2.add(ePanel2,BorderLayout.EAST);
    //................................
    //                               .
    //           GUI3                .
    //................................

    JFrame frame3 = new JFrame ("JLCB Automated Banking Machine v 1.0 ");
    frame3.setPreferredSize (new Dimension(500, 200));
    header3= new JLabel("JLCB Automated Banking Machine");

    JPanel nPanel3= new JPanel();
    nPanel3.add(header1);
    GUI3.add(nPanel3,BorderLayout.NORTH);

    JLabel txt3 =new JLabel("Please select from the buttons on the right");

    JPanel cPanel3= new JPanel();
    cPanel3.add(txt3);
    GUI3.add(cPanel3, BorderLayout.CENTER);

    JPanel wPanel3=new JPanel(new GridLayout(4,0));
    wPanel3.add(bt31);
    wPanel3.add(bt32);
    wPanel3.add(bt33);
    wPanel3.add(bt34);

    JPanel ePanel3=new JPanel(new GridLayout(4,0));
    ePanel3.add(bt35);
    ePanel3.add(bt36);
    ePanel3.add(bt37);
    ePanel3.add(bt38);

    GUI3.add(wPanel3,BorderLayout.WEST);
    GUI3.add(ePanel3,BorderLayout.EAST);
    //..................................
    //                                 .
    //           GUI4                  .
    //..................................

    JFrame frame4 = new JFrame ("JLCB Automated Banking Machine v 1.0 ");
    frame4.setPreferredSize (new Dimension(500, 200));
    header4= new JLabel("JLCB Automated Banking Machine");

    JPanel nPanel4= new JPanel();
    nPanel4.add(header4);
    GUI4.add(nPanel4,BorderLayout.NORTH);


    JPanel cPanel4= new JPanel();
    GUI4.add(cPanel4, BorderLayout.CENTER);

    JPanel wPanel4=new JPanel(new GridLayout(4,0));
    wPanel4.add(bt41);
    wPanel4.add(bt42);
    wPanel4.add(bt43);
    wPanel4.add(bt44);

    JPanel ePanel4=new JPanel(new GridLayout(4,0));
    ePanel4.add(bt45);
    ePanel4.add(bt46);
    ePanel4.add(bt47);
    ePanel4.add(bt48);

    GUI4.add(wPanel4,BorderLayout.WEST);
    GUI4.add(ePanel4,BorderLayout.EAST);

    //---------------------------------------------------

    panelCont.setLayout(cl);

    panelCont.add(GUI1, "1");
    panelCont.add(GUI2, "2");
    panelCont.add(GUI3, "3");
    cl.show(panelCont,"1");


    String a ="100001 1111 15984789.74";


        bt08.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            cl.show(panelCont,"2");
        }
    }); 

        bt28.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent r){
            if (cardNum.equals(a.substring(0, a.indexOf(" "))) &&  pin.equals(a.substring(a.indexOf(" ")+1,a.lastIndexOf(" ")))){
                cl.show(panelCont,"3");
                String acc="a";
            }
            else{
                JOptionPane.showMessageDialog(null,"Invalid accoun number or pin");
                cl.show(panelCont,"3");

            }

        }
    });

           bt35.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            cl.show(panelCont,"3"); 
          }         
    });

        bt36.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            cl.show(panelCont,"3");   

        }
    });

        bt37.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            cl.show(panelCont,"3");   

        }
    });

    frame1.add(panelCont);
    //(JFrame.DO_NOTHING_ON_CLOSE)
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame1.pack();
    frame1.setVisible(true);

}

}

1 个答案:

答案 0 :(得分:0)

问题是你的cardNum和pin的值永远不会在txt和txt2的actionListeners中分配。您应该在用户单击“继续”按钮的情况下提取值。请参阅下面的代码。

        bt28.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent r) {

            cardNum = txt.getText();
            pin = txt2.getText();

            if (cardNum.equals(a.substring(0, a.indexOf(" "))) && pin.equals(a.substring(a.indexOf(" ") + 1, a.lastIndexOf(" ")))) {
                cl.show(panelCont, "3");
                String acc = "a";
            } else {
                JOptionPane.showMessageDialog(null, "Invalid accoun number or pin");
                cl.show(panelCont, "3");

            }

        }
    });