插入记录时出现 Nullpointerexception

get Nullpointerexception while insert records

我尝试使用 prepare 语句将数据插入到我的数据库中,但出现 Nullpointerexception

我的数据库连接代码

public class DBConnect {
    Connection conn=null;

      public static Connection connecrDb(){
          try{
          Class.forName("org.sqlite.JDBC");
          Connection conn = DriverManager.getConnection("jdbc:sqlite:E:\NetBeansProjects\Abdo`s Project\project.sqlite");
              System.out.println("connection success");
              conn.setAutoCommit(false);
          return conn;
          }
          catch(Exception e){
              JOptionPane.showMessageDialog(null, e);
              return null;
          }
      }

}

我的代码在 Main Class

public class Items extends javax.swing.JFrame {


    Connection conn =null;
    ResultSet rs = null;
    PreparedStatement pst=null;
    public Items() {
        initComponents();
        DBConnect.connecrDb();
    }

    private void SaveBtnActionPerformed(java.awt.event.ActionEvent evt) {                                        

        try{


           String sql = "INSERT INTO test (name) values(?)";
           pst=conn.prepareStatement(sql);
           pst.setString(1,item_name.getText() );
           pst.executeUpdate(); 
           conn.commit();
        }
        catch(Exception e ){
                      System.out.println(e);
            System.out.println(e.getStackTrace());  
        }


    } 

输出为

java.lang.NullPointerException

[Ljava.lang.StackTraceElement;@7da469fe

我的代码有什么问题?

conn 在 SaveButtonActionPerformed 中为空。在 Item class:

中初始化
public class Items extends javax.swing.JFrame {

    Connection conn = DriverManager.getConnection("jdbc:sqlite:E:\NetBeansProjects\Abdo`s Project\project.sqlite");

您也可以在您的方法中对其进行初始化,但请务必在使用前对其进行初始化。