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);
希望各位大神能够帮我指出错误