简体   繁体   English

向数据库插入数据:NullPointerException 错误

[英]Inserting data to database: NullPointerException error

Please help, im new to programming so i dont really know what the problem is and how to diagnose it.请帮忙,我是编程新手,所以我真的不知道问题是什么以及如何诊断它。 im trying to write data to the access database which i have created but i keep on getting the following error我试图将数据写入我创建的访问数据库,但我不断收到以下错误

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javaapplication1.Registration.BtnRegActionPerformed(Registration.java:267)
at javaapplication1.Registration.access$300(Registration.java:15)
at javaapplication1.Registration$4.actionPerformed(Registration.java:221)

Here is my Code:这是我的代码:

package javaapplication1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Boosta
 */
public class Registration extends javax.swing.JFrame {

    private static Statement st;
private static ResultSet rs;
private static Connection conn;
private static String selTable;



    public Registration() {
        initComponents();
    }

   public void CreateResultSet(){
    try {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            } catch (ClassNotFoundException ex) {
             Logger.getLogger(Registration.class.getName()).log(Level.SEVERE, null, ex);
            }

     try{
         conn = DriverManager.getConnection("jdbc:odbc:cb1");
         st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

    String sql =  "INSERT INTO Users(Title,FName,SName,IDNO,PAddress,EAddress,Username,Password)" + "VALUES (?,?,?,?,?,?,?,?)";
    //st.execute(sql);
    rs = st.executeQuery(sql);
}
catch(SQLException ex){
  Logger.getLogger(Registration.class.getName()).log(Level.SEVERE, null, ex);  
}

    }

  @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        txtSname = new javax.swing.JTextField();
        txtID = new javax.swing.JTextField();
        txtPA = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtE = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtUsern = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txtPass = new javax.swing.JPasswordField();
        jLabel10 = new javax.swing.JLabel();
        BtnReg = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        txtTitle = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Name");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(40, 90, 40, 15);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Surname");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(40, 130, 50, 15);

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("ID Number");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(40, 170, 60, 15);

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setText("Physical Address");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(30, 210, 90, 15);
        getContentPane().add(txtName);
        txtName.setBounds(130, 80, 100, 30);

        txtSname.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtSnameActionPerformed(evt);
            }
        });
        getContentPane().add(txtSname);
        txtSname.setBounds(130, 120, 100, 30);
        getContentPane().add(txtID);
        txtID.setBounds(130, 160, 190, 30);
        getContentPane().add(txtPA);
        txtPA.setBounds(130, 200, 250, 30);

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(255, 255, 255));
        jLabel5.setText("Title");
        getContentPane().add(jLabel5);
        jLabel5.setBounds(40, 50, 40, 15);

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(255, 255, 255));
        jLabel6.setText("Email Address");
        getContentPane().add(jLabel6);
        jLabel6.setBounds(30, 250, 80, 15);

        txtE.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtEActionPerformed(evt);
            }
        });
        getContentPane().add(txtE);
        txtE.setBounds(130, 240, 160, 30);

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel7.setForeground(new java.awt.Color(255, 255, 255));
        jLabel7.setText("Username");
        getContentPane().add(jLabel7);
        jLabel7.setBounds(30, 290, 70, 15);
        getContentPane().add(txtUsern);
        txtUsern.setBounds(130, 280, 130, 30);

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel8.setForeground(new java.awt.Color(255, 255, 255));
        jLabel8.setText("Password");
        getContentPane().add(jLabel8);
        jLabel8.setBounds(30, 330, 60, 15);

        txtPass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPassActionPerformed(evt);
            }
        });
        getContentPane().add(txtPass);
        txtPass.setBounds(130, 320, 130, 30);

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel10.setForeground(new java.awt.Color(255, 255, 255));
        jLabel10.setText("Registration");
        getContentPane().add(jLabel10);
        jLabel10.setBounds(10, 0, 170, 30);

        BtnReg.setText("Submit");
        BtnReg.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnRegActionPerformed(evt);
            }
        });
        getContentPane().add(BtnReg);
        BtnReg.setBounds(70, 370, 65, 23);

        jButton2.setText("Cancel");
        getContentPane().add(jButton2);
        jButton2.setBounds(170, 370, 65, 23);
        getContentPane().add(txtTitle);
        txtTitle.setBounds(130, 40, 140, 30);

        jLabel9.setIcon(new javax.swing.ImageIcon("C:\\Users\\Boosta\\Pictures\\keyboard-register.jpg")); // NOI18N
        jLabel9.setText("jLabel9");
        getContentPane().add(jLabel9);
        jLabel9.setBounds(0, -20, 410, 440);

        pack();
    }// </editor-fold>                        

    private void txtSnameActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
    }                                        

    private void txtEActionPerformed(java.awt.event.ActionEvent evt) {                                     
        // TODO add your handling code here:
    }                                    

    private void txtPassActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
    }                                       

    private void BtnRegActionPerformed(java.awt.event.ActionEvent evt) {                                       
       //SaveRecord();
        //CreateResultSet();

             String Title = txtTitle.getText();
        String FName = txtName.getText();
        String SName = txtSname.getText();
        String IDNO = txtID.getText();
        String PAddress = txtPA.getText();
        String EAddress = txtE.getText();
        String UserName = txtUsern.getText();
        String Password = txtPass.getText();
 try {

          rs.moveToInsertRow();

          rs.updateString("Title",Title);
          rs.updateString("FName",FName);
          rs.updateString("SName",SName);
          rs.updateString("IDNO",IDNO);
          rs.updateString("PAddress",PAddress);
          rs.updateString("EAddress",EAddress);
          rs.updateString("UserName",UserName);
          rs.updateString("Password",Password);

          rs.insertRow();   

          st.close();
          rs.close();

    st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);  
    String sql = "INSERT INTO Users(Title,FName,SName,IDNO,PAddress,EAddress,Username,Password)" + "VALUES (?,?,?,?,?,?,?,?)";
    rs = st.executeQuery(sql);

    rs.next();
    txtTitle.setText(rs.getString("Title"));
    txtName.setText(rs.getString("FName"));
    txtSname.setText(rs.getString("SName"));
    txtID.setText(rs.getString("IDNO"));
    txtPA.setText(rs.getString("PAddress"));
    txtE.setText(rs.getString("EAddress"));
    txtUsern.setText(rs.getString("UserName"));
    txtPass.setText(rs.getString("Password"));

 } catch (SQLException ex) {
             //Logger.getLogger(Registration.class.getName()).log(Level.SEVERE, null, ex);
            }




    }                                      

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Registration.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Registration.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Registration.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Registration.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Registration().setVisible(true);

            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton BtnReg;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField txtE;
    private javax.swing.JTextField txtID;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtPA;
    private javax.swing.JPasswordField txtPass;
    private javax.swing.JTextField txtSname;
    private javax.swing.JTextField txtTitle;
    private javax.swing.JTextField txtUsern;
    // End of variables declaration                   
}

You conn variable is null.您的conn变量为空。 You are initializing it in your method CreateResultSet() , which seems to be never called since it´s commented out just a few lines before the exception would occur.您正在您的方法CreateResultSet()对其进行初始化,该方法似乎从未被调用,因为它在异常发生前仅注释了几行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM