简体   繁体   中英

Java Swing gui designer button click throwing error

I'm using Java swing to design a application and I'm trying to have it so that when a button is clicked the contents of 4 textfields are added to an array list and passed to a method to store in mysql. after looking for an answer to my problem I found this : Adding numbers to an arraylist from the Jtextfield and I seem to have done everything right but it still doesn't work.

gui button and 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);

and the method to store with mysql if it's needed (Via Connector 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;
        }
    }
}

But I'm still figuring out the mysql, I was attempting to do a test of it.

and this is the error I'm getting: (points to 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)

The most probable reason of you getting a NullPointerException is that you haven't initialized your list inside the ActionListener of your button

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

Here, instead of

public List<String> user_array;

use

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM