Java组合框控件获取数据的问题(新人求带)
    package save;

    import java.awt.event.ActionEvent;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;

    import frame.Frame;

    public class Save{
        Frame fm=new Frame();
        public void actionPerformed(ActionEvent e){
                try{
                    Class.forName("com.mysql.jdbc.Driver");
                    Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/stuDB?useUnicode=true&characterEncoding=utf8","root","");
                    System.out.println("成功连接到数据库!");
                    PreparedStatement stmt =conn.prepareStatement("insert into stuDB values(?,?,?,?)");
                    stmt.setString(1, fm.getIdFd().getText());
                    stmt.setString(2, fm.getNameFd().getText());
                    stmt.setString(3, fm.getSexBox().getSelectedItem().toString());
                    stmt.setInt(4, Integer.parseInt(fm.getAgeFd().getText()));
                    fm.getIdFd().setText("");
                    fm.getNameFd().setText("");
                    fm.getAgeFd().setText("");
                }
                catch (ClassNotFoundException e2) {
                    System.out.println("Sorry,can't find the Driver!");
                    e2.printStackTrace();
                } catch (SQLException e3) {
                    e3.printStackTrace();
                } catch (Exception e4) {
                    e4.printStackTrace();
                }
            }
        }


以上是保存按钮实现的代码。尝试了很多方法,只要涉及到getSelectedItem的语句一定会出现问题。
以下是报错:
java.lang.NullPointerException
at save.Save.actionPerformed(Save.java:15)
at frame.Frame$2.actionPerformed(Frame.java:141)
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$500(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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.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$JavaSecurityAccessImpl.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)

附定义代码:
    JComboBox<String> sexBox = new JComboBox<String>();
    sexBox.setMaximumRowCount(8);
    sexBox.setModel(new DefaultComboBoxModel<String>(new String[] {"男", "女"}));
    sexBox.setBounds(81, 64, 65, 21);
    frame.getContentPane().add(sexBox);

    希望各位大神能够帮我指出错误

4个回答

或者有没有和JComboBox外观相近的控件可以代替呢?

save的第十五行有错误at save.Save.actionPerformed(Save.java:15),,是不是数据库没连啊

Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/stuDB?useUnicode=true&characterEncoding=utf8","root","");
System.out.println("成功连接到数据库!");
PreparedStatement stmt =conn.prepareStatement("insert into stuDB values(?,?,?,?)");
这个地方空指针,conn为空,数据库正常开启了没

数据库显示连接成功,前两个文本框是可以执行的,执行第三个的时候才会跳转至e4

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问