Java Swing gui设计器按钮点击抛出错误

时间:2014-07-07 17:51:20

标签: java swing

我正在使用Java swing设计一个应用程序而我正试图让它在单击按钮时将4个文本字段的内容添加到数组列表并传递给存储的方法在mysql中。在寻找我的问题的答案后,我发现了这个:Adding numbers to an arraylist from the Jtextfield我似乎已经做好了一切,但它仍然没有用。

gui button和tetfields:

    ID = new JTextField();
    ID.setBounds(20, 36, 46, 20);
    panel.add(ID);
    ID.setColumns(10);

    email = new JTextField();
    email.setBounds(191, 36, 109, 20);
    panel.add(email);
    email.setColumns(10);

    pass = new JTextField();
    pass.setBounds(319, 36, 96, 20);
    panel.add(pass);
    pass.setColumns(10);

    name = new JTextField();
    name.setColumns(10);
    name.setBounds(86, 36, 86, 20);
    panel.add(name);

    JButton btnadd = new JButton("Add");
    btnadd.addActionListener(new ActionListener() {
        public List<String> user_array;

        @Override
        public void actionPerformed(ActionEvent e) {

            if (!ID.getText().trim().equals("")){
                user_array.add(ID.getText().trim());
            }
            if (!email.getText().trim().equals("")){
                user_array.add(email.getText().trim());
            }
            if (!pass.getText().trim().equals("")){
                user_array.add(pass.getText().trim());
            }
            if (!name.getText().trim().equals("")){
                user_array.add(name.getText().trim());
            }
            //make these depend on wether array is filled
            if(!user_array.isEmpty())
            {
                String[] user = user_array.toArray(new String[5]);
                Lottery.employees.addEmployeeMysql(user);
            }
        }
    });
    btnadd.setBounds(404, 222, 56, 23);
    panel.add(btnadd);

和mysql存储的方法(如果需要)(通过连接器J)

    public static void addEmployeeMysql(String[] user) {
    String[] xsettings = null;
    try {
        xsettings = Lottery.properties.readConfig();
    } catch (ParserConfigurationException | SAXException | IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        conn = DriverManager.getConnection("jdbc:mysql://"+ xsettings[0] +"/"+ xsettings[4] +"?user="+ xsettings[2] +"&password="+ xsettings[3]);
        stmt = conn.createStatement();
        rs = stmt.executeQuery("INSERT INTO employees (ID,name,email,password) VALUES ("+ user[0] +","+ user[1] +","+ user[2] +","+ user[3] +")");

    } catch (Exception ex) {
        // handle the error
    }finally {

        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException sqlEx) { } // ignore

            rs = null;
        }

        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException sqlEx) { } // ignore

            stmt = null;
        }
    }
}

但是我还在搞清楚mysql,我试图对它进行测试。

这是我得到的错误:(指向user_array.add(ID.getText().trim());

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Lottery.gui$3.actionPerformed(gui.java:378)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

1 个答案:

答案 0 :(得分:1)

获得NullPointerException的最可能原因是您没有在按钮的ActionListener初始化您的列表

btnadd.addActionListener(new ActionListener() {
    public List<String> user_array;
    @Override
    public void actionPerformed(ActionEvent e) {...}

此处,而不是

public List<String> user_array;

使用

public List<String> user_array = new ArrayList<String>();