0

I am trying to set up a method to convert java.util.date typed variables into java.sql.date typed variables so they can be persisted to database. However, the code I set up keeps returning a null pointer exception, even though I am getting values on the argument being passed into the method.

public static java.sql.Date convertUtilDateSQLDate(java.util.Date utilDate){

        long utilDateTime = utilDate.getTime();

        java.sql.Date sqlDate = new java.sql.Date(utilDateTime);

        return sqlDate;

    }

Does anyone know why this might be happening?

Declan
  • 448
  • 10
  • 27
  • Can you please verify that `utilDate` is not null? – akhil_mittal May 08 '15 at 12:34
  • You are probably giving it a null argument. – RealSkeptic May 08 '15 at 12:34
  • utilDate is not null; I have even tested the value of long utilDateTime before instantiating the sqlDate object, and it has a long value. – Declan May 08 '15 at 12:45
  • You are correct, I have identified that there is a null in the pipeline - I am having an issue with reading in an Excel date into the java.util.date using the Apache POI - the cell.getDateCellValue is not reading it in as a date. – Declan May 08 '15 at 13:01
  • Figured out the issue was that as I was reading down the Excel file rows inside of a while loop using rows.next(), it was reading blank rows even though there were no values in the cells. – Declan May 08 '15 at 14:14

1 Answers1

1

The utilDate object might be null. Include NULL check condition to avoid it. like.

public static java.sql.Date convertUtilDateSQLDate(java.util.Date utilDate){

         if( utilDate == null){
              //add log and return;
              return null;
          }
            long utilDateTime = utilDate.getTime();
            java.sql.Date sqlDate = new java.sql.Date(utilDateTime);
            return sqlDate;

        }

If utilDate is null, then this method returns the NULL back, so the method which invokes this convertUtilDateSQLDate() method should also do the NULL check.

K139
  • 3,654
  • 13
  • 17