Java空指针异常通过GUI和MYSQL输入到phpmyadmin中的数据库

标签 java mysql database swing phpmyadmin

我正在尝试通过使用 GUI 和 MYSQL 代码向数据库添加输入。但我不断收到“java.lang.NullPointerException”并且它可能不是修复后的最后一个错误..无论如何。按下按钮应该从您输入的文本框中获取输入,并将其发送到数据库,但它不太有效。 你能帮助我吗? 问题出在actionPerformed方法中

(如果有帮助,您可以复制粘贴代码并直接运行)

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);
    }
}
}       
}

最佳答案

很可能您在连接数据库时遇到问题,因此您的连接对象“con”为空,因此 java.lang.NullPointerException

java:119 at PreparedStatement preparedStatement=con.prepareStatement(sql);

关于Java空指针异常通过GUI和MYSQL输入到phpmyadmin中的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26016382/

相关文章:

php - Codeigniter 数据库结果 - 对象还是数组?

java - 调用方法完全没有任何作用?

java - 在 Android 中,一个应用程序在较低 API 级别出现编译错误的情况下是否可以顺利运行?

java - Java 中详尽的嵌套目录搜索

Java Spring Boot : How to map my app root (“/” ) to index. html?

php - PHP/MySQL 插入问题

mysql - 更新 MySql 数据库中同一张表的字段

sql - sql中firebase的集合和文档的等价物是什么?

php - WordPress MySQL 错误日志

python - Heroku - 无法连接到本地 MySQL 服务器