2014-09-25 88 views
-1

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

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

你有什么尝试? NullPointerException告诉你哪一行代码有问题,所以首先看看那里。 – 2014-09-25 11:56:01

+0

嘿理查德感谢您的评论。我试过运行调试,以及它并没有说在哪里行therpoidpointer异常发生在..虽然我认为它是在actionPerformed方法的地方.. – Stigblue 2014-09-25 12:06:50

+0

你可以发布stacktrace? – 2014-09-25 12:44:11

回答

0

假如你在数据库表检查NOT NULL字段复制输出窗口中的东西。这就是堆栈跟踪。

+0

嗯,它只是在输出窗口中显示“java.lang.NullPointerException”..:P – Stigblue 2014-09-25 20:08:18

+0

可以添加输出窗口屏幕截图 – 2014-09-26 04:05:11

+0

屏幕截图:https://imageshack.com/i/expurbkYp – Stigblue 2014-09-26 08:03:53