0

I have:

String stringDate = "2017-02-16T15:00:00Z"

I want to convert this into a Date and after it i want to be converted to Long. Here is my code:

 private void normalizeDate(ContentValues values) {
    // normalize the date value
    if (values.containsKey(SmogContract.MeasurementEntry.COLUMN_FROM_DATE_TIME)) {
        Date date = convertDateFromStringToDate(values.getAsString(SmogContract.MeasurementEntry.COLUMN_FROM_DATE_TIME));
        long fromDateValue = date.getTime();
        values.put(SmogContract.MeasurementEntry.COLUMN_FROM_DATE_TIME, SmogContract.normalizeDate(fromDateValue));
    }
    if (values.containsKey(SmogContract.MeasurementEntry.COLUMN_TILL_DATE_TIME)) {
        long fromDateValue = values.getAsLong(SmogContract.MeasurementEntry.COLUMN_TILL_DATE_TIME);
        values.put(SmogContract.MeasurementEntry.COLUMN_TILL_DATE_TIME, SmogContract.normalizeDate(fromDateValue));
    }
}

private Date convertDateFromStringToDate(String stringDate){
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    format.setTimeZone(TimeZone.getTimeZone("GMT"));
    Date convertedFromStringDate = null;
    try {
        convertedFromStringDate = format.parse(stringDate);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return convertedFromStringDate;
}

Here is the exception that i am getting:

java.lang.NullPointerException: Attempt to invoke virtual method 'long java.lang.Long.longValue()' on a null object reference
                 at com.example.marcin.smog_mapa.data.SmogProvider.normalizeDate(SmogProvider.java:109)
                 at com.example.marcin.smog_mapa.data.SmogProvider.insert(SmogProvider.java:85)
                 at android.content.ContentProvider$Transport.insert(ContentProvider.java:263)
                 at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:163)
                 at android.os.Binder.execTransact(Binder.java:453)
Jan
  • 2,060
  • 2
  • 29
  • 34
wegtis
  • 303
  • 2
  • 4
  • 12
  • Please have a look at console. Surprise !!! – Suresh Atta Feb 17 '17 at 13:03
  • 1
    http://stackoverflow.com/questions/19112357/java-simpledateformatyyyy-mm-ddthhmmssz-gives-timezone-as-ist – Laazo Feb 17 '17 at 13:08
  • 1
    Possible duplicate of [Java SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") gives timezone as IST](http://stackoverflow.com/questions/19112357/java-simpledateformatyyyy-mm-ddthhmmssz-gives-timezone-as-ist) – RamPrakash Feb 17 '17 at 13:09
  • Well, with that input, this should be fine since you don't really have a `Z` value. – AxelH Feb 17 '17 at 13:11
  • I don't know what you expect but it returns: `Thu Feb 16 15:00:00 PKT 2017` not null at all – SSC Feb 17 '17 at 13:14
  • I got a correct result using your method, and no exception thrown. Could you provide more information about how your method `convertDateFromStringToDate` is used? e.g. your full program. – Mincong Huang Feb 17 '17 at 13:21
  • @AxelH I have made an edit of the question – wegtis Feb 17 '17 at 13:25
  • You exception message is not the root cause. Please provide the full stack trace. – Mincong Huang Feb 17 '17 at 13:37

3 Answers3

0

The format.parse(...) statement fails with a ParseException, leaving convertedFromStringDate at null. Check your console for the stack trace and make sure stringDate has the correct format.

K. Zander
  • 36
  • 1
  • 4
0

OK, so it was just an silly mistake in the method.

Here is it done correctly:

private void normalizeDate(ContentValues values) {
    // normalize the date value
    if (values.containsKey(SmogContract.MeasurementEntry.COLUMN_FROM_DATE_TIME)) {
        Date date = convertDateFromStringToDate(values.getAsString(SmogContract.MeasurementEntry.COLUMN_FROM_DATE_TIME));
        Log.d("ConvertedDate: ", String.valueOf(date.getTime()));
        long fromDateValue = date.getTime();
        values.put(SmogContract.MeasurementEntry.COLUMN_FROM_DATE_TIME, SmogContract.normalizeDate(fromDateValue));
    }
    if (values.containsKey(SmogContract.MeasurementEntry.COLUMN_TILL_DATE_TIME)) {
        Date date = convertDateFromStringToDate(values.getAsString(SmogContract.MeasurementEntry.COLUMN_TILL_DATE_TIME));
        Log.d("ConvertedDate: ", String.valueOf(date.getTime()));
        long fromDateValue = date.getTime();
        values.put(SmogContract.MeasurementEntry.COLUMN_TILL_DATE_TIME, SmogContract.normalizeDate(fromDateValue));
    }
}
wegtis
  • 303
  • 2
  • 4
  • 12
0

The problem come from

long fromDateValue = values.getAsLong(SmogContract.MeasurementEntry.COLUMN_TILL_DATE_TIME);

Where values is a ContentValues instance.

ContentValues.getAsLong return a null that you store in a long, so it call Long.longValue() leading to this NullPointerException

This is the risk with this auto-boxing / unboxing, if you have

Long wrap_long = null;
long l = wrap_long;

This will compile but will throw a NPE at runtime where

long l = null; 

will never compile, for the same reason, a primitive value can't be null

It is good to check the Long value for null when you unbox it.

AxelH
  • 14,325
  • 2
  • 25
  • 55