简体   繁体   English

Java getTime()从格式化的字符串返回null

[英]Java getTime() returns null from formatted String

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. 我想将其转换为日期,然后将其转换为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)

The format.parse(...) statement fails with a ParseException , leaving convertedFromStringDate at null . format.parse(...)语句失败,并出现ParseException ,将convertedFromStringDate保留为null Check your console for the stack trace and make sure stringDate has the correct format. 检查您的控制台的堆栈跟踪,并确保stringDate具有正确的格式。

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

The problem come from 问题来自

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

Where values is a ContentValues instance. 其中valuesContentValues实例。

ContentValues.getAsLong return a null that you store in a long , so it call Long.longValue() leading to this NullPointerException ContentValues.getAsLong返回存储在longnull ,因此它调用Long.longValue()导致此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 这将编译,但会在运行时抛出NPE,其中

long l = null; 

will never compile, for the same reason, a primitive value can't be null 出于相同的原因,它将永远不会编译,原始值不能为null

It is good to check the Long value for null when you unbox it. 取消装箱时,最好检查Long值是否为null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM