2017-02-17 5 views
0

ich habe:Java getTime() gibt null von formatierten String

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

ich dies in ein Datum konvertiert werden soll und nach möchte ich nach Long umgewandelt werden. Hier ist mein 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; 
} 

Hier ist die Ausnahme, die ich erhalte:

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) 
+0

Bitte schauen Sie sich die Konsole an. Überraschung !!! –

+1

http://stackoverflow.com/questions/19112357/java-simpledateformatyyyy-mm-ddthhmmssz-gives-timezone-as-ist – Laazo

+1

Mögliches Duplikat von [Java SimpleDateFormat ("yyyy-MM-dd'T'HH: mm: ss ‚Z‘ ") gibt Zeitzone als iST] (http://stackoverflow.com/questions/19112357/java-simpledateformatyyyy-mm-ddthhmmssz-gives-timezone-as-ist) – RamPrakash

Antwort

0

Die format.parse(...) Anweisung schlägt mit einem ParseException, convertedFromStringDate bei null verlassen. Überprüfen Sie Ihre Konsole auf die Stack-Trace und stellen Sie sicher, stringDate hat das richtige Format.

+0

Dies ist ein String, den ich von einer API-Antwort bekomme – wegtis

0

OK, es war nur ein dummer Fehler in der Methode.

Hier ist es richtig gemacht:

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

Das Problem

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

von

kommen Wo values eine ContentValues Instanz ist.

ContentValues.getAsLong Rückkehr ein null, die Sie in einem long speichern, so dass es Long.longValue() zu dieser führenden nennen NullPointerException

Dieses Risiko ist mit diesem Auto-Boxen/Unboxing, wenn Sie

Long wrap_long = null; 
long l = wrap_long; 

Diese kompiliert, sondern eine NPE zur Laufzeit werfen, wo

long l = null; 

aus demselben Grund kann ein primitiver Wert nicht null

Long Wert für null sein, wenn Sie es auspacken.

Verwandte Themen