NullPointerException - 为什么?

NullPointerException - why?

我遇到空指针异常。我在 ms 访问 table 中使用了长文本数据类型来获取日期和描述

 try
         {
         int z1=1,z2=2;
             String s=JOptionPane.showInputDialog("enter the date(DD/MM/YEAR)");
         String s1=JOptionPane.showInputDialog("enter data into your dairy");
         pstmt=conn.prepareStatement(" INSERT  INTO  Table2(date,description) values(?,?)");
         pstmt.setString(z1, s);
         pstmt.setString(z2, s1);
         pstmt.executeUpdate();
         JOptionPane.showMessageDialog(null, "Data enterd successfully");
         update1();
         close();
         }

         catch(Exception e1)
         {
             JOptionPane.showMessageDialog(null, "Data Insertion failed.This date may already exist or may be some other error");
             JOptionPane.showMessageDialog(null, "ERROR:"+e1.getMessage());

         }

我在 MS 访问中使用数据类型长文本作为日期和描述 table。

将您的代码替换为

int z1=1,z2=2;  
String s="14/04/2015";  
String s1="test";  
if ( conn != null ) {
    pstmt=conn.prepareStatement(" INSERT  INTO  Table2(date,description) values(?,?)");
    if ( pstmt != null ) {
        pstmt.setString(z1, s);
        pstmt.setString(z2, s1);
        pstmt.executeUpdate();
    }
    else {
        System.out.printl("pstmt is null!");
    }
}
else {
    System.out.printl("conn is null!");
}

并将输出复制到这里。

在这种情况下,唯一可能的 null 变量可以是 conn 所有其他变量都使用 文字 或使用 方法实例化不会 return null (如果有问题他们会抛出异常)。