0

Here i have executed the following code and parsed it to date format from form.

        Date formatter = new SimpleDateFormat("mm/dd/yyyy");
        SimpleDateFormat newFormat = new SimpleDateFormat("yyyy/mm/dd");
        Date date = null,sdate=null;
        String datef = null;
        try {
            date =formatter.parse(datestring);
            sdate = newFormat.format(date);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        ps=conn.prepareStatement("INSERT INTO work_schedule (date) VALUE(?)"); // connection conn defined.
    ps.setDate(1,(java.sql.Date.valueOf(sdate)));

The above code throws a null pointer exception , i tried executing query in my sql workbench query works good.

Please let me know if i have made any error in the above code.

avinash v p
  • 531
  • 2
  • 8
  • 15
  • Next, read the documentation for `SimpleDateFormat` carefully, and stop "handling" exceptions by just ignoring them. (Also, the code you've written won't even compile...) – Jon Skeet Mar 02 '16 at 06:59
  • I think there went something wrong in your parse and firnat try and catch... Is there an exceltion thrown in the try block ? – Simon Ludwig Mar 02 '16 at 07:00
  • nope the parsing is good have tried that too..im using mvc and so the error is thrown in my database class method.. which only has the preparedstatement and connection . – avinash v p Mar 02 '16 at 07:01
  • So you should debug your code to find out which object is null. – Simon Ludwig Mar 02 '16 at 07:02
  • on it .. was having some issues with eclipse debugger just reformatted it, will try fixing it once more. – avinash v p Mar 02 '16 at 07:05
  • sorry everything was right forgot to initialize conn my bad sorry will delete this post – avinash v p Mar 02 '16 at 07:20

0 Answers0