0

I am getting a Null Pointer Exception. And i have used the long text data type in the ms access table to for date and description

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

         }

I have used data type long text for date and description in the ms access table.

StormeHawke
  • 5,534
  • 5
  • 38
  • 68

2 Answers2

0

Replace your code by

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

And copy the output here.

StephaneM
  • 4,364
  • 1
  • 14
  • 32
0

In this case the only possible null variable can be conn all the other variables are instantiated using literals or using methods that don't return null (they will throw exceptions if something is wrong).

Titus
  • 20,544
  • 1
  • 19
  • 29