Java 插入操作时的NullPointerException

Java 插入操作时的NullPointerException,java,swing,nullpointerexception,Java,Swing,Nullpointerexception,我试图通过按钮的actionPerformed方法插入数据库。 但是我一直得到“java.lang.NullPointerException”,按钮按下应该从文本框插入并发送到数据库,但是我目前在actionperformed方法中得到一个java空指针异常。你能帮助我吗? (如果有帮助,可以复制粘贴代码并直接运行) (有到数据库的连接,所以不是这样) 您是否检查了数据库表中的NOTNULL字段。复制输出窗口中的内容。这是堆栈跟踪。您尝试了什么?NullPointerException会告诉您哪

我试图通过按钮的actionPerformed方法插入数据库。 但是我一直得到“java.lang.NullPointerException”,按钮按下应该从文本框插入并发送到数据库,但是我目前在actionperformed方法中得到一个java空指针异常。你能帮助我吗? (如果有帮助,可以复制粘贴代码并直接运行) (有到数据库的连接,所以不是这样)


您是否检查了数据库表中的NOTNULL字段。复制输出窗口中的内容。这是堆栈跟踪。

您尝试了什么?NullPointerException会告诉您哪行代码有问题,因此请从那里开始查看。嘿,Richard,谢谢您的评论。我也试过运行debug,但它并没有说在哪一行发生了null指针异常。。虽然我假设它在actionPerformed方法的某个地方..你能发布stacktrace吗?它只是在输出窗口中清楚地说“java.lang.NullPointerException…”PCU是否可以添加输出窗口的屏幕截图
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JToolBar;
import javax.swing.JDesktopPane;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.*;
import javax.swing.JPasswordField;
import javax.swing.JList; 
import javax.swing.JComboBox;


public class MyGuiApplication {


//database globals
private Connection con;
private Statement st;
private ResultSet rs;   

//gui globals
private JFrame frame;
private JTextField EmailTextField;
private JTextField FirstNameTextField;
private JTextField LastNameTextField;
private JTextField MobileNumberTextField;
private JLabel lblPassword;
private JLabel lblConfirmPassword;
private JRadioButton MrsMsBTN;
private JRadioButton MrBTN;
private JLabel lblTitle;
private JLabel lblFirstName;
private JLabel lblLastName;
private JLabel lblMobileNumber;
private JLabel lblNewCustomer;
private JLabel lblDeliveryInformation;
private JLabel lblInCaseWe;
private JLabel lblMandatoryField;
private JPasswordField ConfirmPasswordField;
private JPasswordField PasswordField;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MyGuiApplication window = new MyGuiApplication();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public MyGuiApplication() {
    initialize();
    DBConnect();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 456, 560);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    //KNAPPEN
    JButton btnUpdate = new JButton("update");
    //ACTION LISTENER - TILFØJ DATA TIL DATABASEN VED TRYK PÅ KNAP
    btnUpdate.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) 

        {           
            try
            {                   
                String sql="INSERT INTO customers (Email_Address, Password, User_ID, Title_Mr/Mrs, First_Name, Last_Name, Phone_Number, How_did_you_find_us?, Agree_to_terms_&_conditions, Receive_mails_and_offers?) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
                PreparedStatement preparedStatement=con.prepareStatement(sql);
                preparedStatement.setString (1, EmailTextField.getText()); // email addresse
                preparedStatement.setString (2, PasswordField.getText()); // Stemme overens med ConfirmPasswordField?           
                preparedStatement.setInt    (3, ((Integer) null)); //user ID primary key auto increment
                preparedStatement.setString (4, "Mr"); // Title mrs/mr
                preparedStatement.setString (5, FirstNameTextField.getText());
                preparedStatement.setString (6, LastNameTextField.getText());
                preparedStatement.setString (7, MobileNumberTextField.getText());
                preparedStatement.setString (8, null); //dropdown menu, "how did you find us"?
                preparedStatement.setBoolean(9, true); // agree to terms
                preparedStatement.setBoolean(10, true); // receive email offers from us

                preparedStatement.executeUpdate();


            } catch (Exception e) {
                System.out.print(e);

            }               
        }
    });
    btnUpdate.setBounds(138, 474, 89, 23);
    frame.getContentPane().add(btnUpdate);


    EmailTextField = new JTextField();
    EmailTextField.setBounds(163, 68, 186, 20);
    frame.getContentPane().add(EmailTextField);
    EmailTextField.setColumns(10);

    FirstNameTextField = new JTextField();
    FirstNameTextField.setBounds(163, 241, 186, 20);
    frame.getContentPane().add(FirstNameTextField);
    FirstNameTextField.setColumns(10);

    LastNameTextField = new JTextField();
    LastNameTextField.setBounds(163, 286, 186, 20);
    frame.getContentPane().add(LastNameTextField);
    LastNameTextField.setColumns(10);

    MobileNumberTextField = new JTextField();
    MobileNumberTextField.setBounds(163, 331, 186, 20);
    frame.getContentPane().add(MobileNumberTextField);
    MobileNumberTextField.setColumns(10);

    JCheckBox TermsConditionsCheckBox = new JCheckBox("Yes, i agree to the Terms and Conditions.*");
    TermsConditionsCheckBox.setBounds(115, 418, 266, 23);
    frame.getContentPane().add(TermsConditionsCheckBox);

    JCheckBox EmailOffersCheckBox = new JCheckBox("Yes, i wish to receiver Email offers from Zalando.");
    EmailOffersCheckBox.setBounds(115, 444, 319, 23);
    frame.getContentPane().add(EmailOffersCheckBox);

    JLabel lblEmailAddress = new JLabel("Email Address*");
    lblEmailAddress.setBounds(47, 71, 106, 14);
    frame.getContentPane().add(lblEmailAddress);

    lblPassword = new JLabel("Password*");
    lblPassword.setBounds(65, 112, 88, 14);
    frame.getContentPane().add(lblPassword);

    lblConfirmPassword = new JLabel("Confirm password*");
    lblConfirmPassword.setBounds(25, 143, 128, 14);
    frame.getContentPane().add(lblConfirmPassword);

    MrsMsBTN = new JRadioButton("Mrs./Ms.");
    MrsMsBTN.setSelected(true);
    MrsMsBTN.setBounds(138, 211, 76, 23);
    frame.getContentPane().add(MrsMsBTN);

    MrBTN = new JRadioButton("Mr.");
    MrBTN.setBounds(216, 211, 109, 23);
    frame.getContentPane().add(MrBTN);

    lblTitle = new JLabel("Title*");
    lblTitle.setBounds(95, 216, 37, 14);
    frame.getContentPane().add(lblTitle);

    lblFirstName = new JLabel("First name*");
    lblFirstName.setBounds(65, 244, 88, 14);
    frame.getContentPane().add(lblFirstName);

    lblLastName = new JLabel("Last name*");
    lblLastName.setBounds(65, 289, 88, 14);
    frame.getContentPane().add(lblLastName);

    lblMobileNumber = new JLabel("Mobile Number");
    lblMobileNumber.setBounds(47, 334, 106, 14);
    frame.getContentPane().add(lblMobileNumber);

    lblNewCustomer = new JLabel("New Customer");
    lblNewCustomer.setFont(new Font("Tahoma", Font.BOLD, 16));
    lblNewCustomer.setBounds(25, 37, 149, 14);
    frame.getContentPane().add(lblNewCustomer);

    lblDeliveryInformation = new JLabel("Delivery information");
    lblDeliveryInformation.setFont(new Font("Tahoma", Font.PLAIN, 12));
    lblDeliveryInformation.setBounds(10, 181, 109, 14);
    frame.getContentPane().add(lblDeliveryInformation);

    lblInCaseWe = new JLabel("In case we need to contact you about your order");
    lblInCaseWe.setFont(new Font("Tahoma", Font.PLAIN, 10));
    lblInCaseWe.setBounds(163, 362, 251, 14);
    frame.getContentPane().add(lblInCaseWe);

    lblMandatoryField = new JLabel("* mandatory field");
    lblMandatoryField.setFont(new Font("Tahoma", Font.ITALIC, 11));
    lblMandatoryField.setBounds(25, 478, 100, 14);
    frame.getContentPane().add(lblMandatoryField);

    ConfirmPasswordField = new JPasswordField();
    ConfirmPasswordField.setBounds(163, 142, 186, 20);
    frame.getContentPane().add(ConfirmPasswordField);

    PasswordField = new JPasswordField();
    PasswordField.setBounds(163, 112, 186, 20);
    frame.getContentPane().add(PasswordField);

    JComboBox comboBox = new JComboBox();
    comboBox.setBounds(163, 391, 186, 20);
    //comboBox.add("Facebook.com", comboBox);
    frame.getContentPane().add(comboBox);

    JLabel lblHowDidYou = new JLabel("How did you find us?*");
    //ADD OPTIONS TO WHERE YOU FOUND US
    lblHowDidYou.setBounds(25, 394, 128, 14);
    frame.getContentPane().add(lblHowDidYou);
}

    public void DBConnect(){
    try{

        Class.forName("com.mysql.jdbc.Driver");

        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/zalando", "root","");
        st = con.createStatement();     

    }catch(Exception ex){
        System.out.println("Error: " +ex);
    }
}
}       
}