0

I've written a code in swing in which I've inserted text in jTextFields. But when I try to insert them in my database, it gives NullPointerException after "Driver Connected". Following is my JDBC code:

    Connection conn = null;
    PreparedStatement pst = null;
    ResultSet rs = null;
    try{

        Class.forName("oracle.jdbc.OracleDriver");
        System.out.println("Driver Loaded.");
        conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",                           "usname", "pass");
        System.out.println("Driver Connected.");

        String insert = "INSERT INTO directorsdb values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        pst=conn.prepareStatement(insert);

        pst.setString(1, dirname.getText());
        pst.setString(2, jDateChooser1.getDate().toString());
        pst.setString(3, DIN.getText());
        pst.setString(4, PAN.getText());
        pst.setString(5, UIN.getText());
        pst.setString(6, birthplace.getText());
        pst.setString(7, passportno.getText());
        pst.setString(8, dlicense.getText());
        pst.setString(9, nationality.getText());
        pst.setString(10, occupation.getText());
        pst.setString(11, occupationcompany.getText());
        pst.setString(12, designation.getText());
        pst.setString(13, qualification.getText());
        pst.setString(14, officeadd.getText());
        pst.setString(15, residentialadd.getText());
        pst.setString(16, contacthome.getText());
        pst.setString(17, contactwork.getText());
        pst.setString(18, contactmobile.getText());
        pst.setString(19, emailper.getText());
        pst.setString(20, emailwork.getText());
        pst.setString(21, emailother.getText());
        pst.setString(22, secretaryname.getText());
        pst.setString(23, secretarycontact.getText());
        pst.setString(24, secretaryemail.getText());
        pst.setString(25, buttonGroup1.getSelection().getActionCommand());
        pst.setString(26, jDateChooser2.getDate().toString());
        pst.setString(27, fathername.getText());
        pst.setString(28, mothername.getText());
        pst.setString(29, jDateChooser3.getDate().toString());
        pst.setString(30, posiionboard.getText());
        pst.setString(31, jDateChooser4.getDate().toString());
        pst.setString(32, cessationreason.getText());
        pst.setString(33, buttonGroup2.getSelection().getActionCommand());
        pst.setString(34, buttonGroup3.getSelection().getActionCommand());
        pst.setString(35, aboutd.getText());

        pst.execute();


    }catch(SQLException e){
        System.err.println(e.fillInStackTrace());
    }
     catch(ClassNotFoundException ex){
         System.err.println(ex);
     } finally{                                 // != means NOT EQUAL TO
        if (rs != null){
            try {
                rs.close();
            } catch (SQLException ex) {
                System.err.println(ex);
            }

            }
        if (pst != null){
            try {
                pst.close();
            } catch (SQLException ex) {
                System.err.println(ex);
            }
        }
        if (conn != null){
            try {
                conn.close();
            } catch (SQLException ex) {
                System.err.println(ex);
            }
         }
     }

Following is the Exception:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at DirectorDB.NewEntry.jButton1ActionPerformed(NewEntry.java:882)
at DirectorDB.NewEntry.access$1800(NewEntry.java:28)
at DirectorDB.NewEntry$19.actionPerformed(NewEntry.java:738)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
user3689611
  • 9
  • 1
  • 1
  • 2

1 Answers1

1

What appears at line 882 of NewEntry?

Reply...

pst.setString(2, jDateChooser1.getDate().toString())

Then I would suggest that jDateChooser1.getDate() is null.

You shouldn't be setting date fields using String, the database should be setup to use an appropriate Date/Time data type and you should try to use pst.setDate(...) instead.

This will store the Date/Time value in a manner which is easily loaded. You should note Date#toString will store the date in a format specified by the current local, making it difficult to load and parse

If you can't use an actual date value in the database, then you should be using something more like...

Date value = jDateChooser1.getDate();
pst.setString(2, value == null ? null : value.toString());

Even better, use a SimpleDateFormat to format the String value into an agreeded format

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366