Cannot invoke method public abstract float java.lang.Number.floatValue() on null

  • Platform information:
    • openHAB version: 5.2.3

I have the follow rule:

rule "Wasserverbrauch Tag Woche Monat Jahr"                                                              
when
    Item rawwater changed 
then
    val ZonedDateTime zdt            = ZonedDateTime.now()                                              // jetzt
    val ZonedDateTime start_of_day   = zdt.with(LocalTime.MIDNIGHT)                                     // heute, Mitternacht
    val ZonedDateTime start_of_week  = start_of_day.minusDays(start_of_day.getDayOfWeek.getValue - 1)   // Montag 
    val ZonedDateTime start_of_month = start_of_day.withDayOfMonth(1)                                   // Erster Tag des Monats
    val ZonedDateTime start_of_year  = start_of_day.withDayOfYear(1)                                    // Erster Tag des Jahres

    if ((rawwater.deltaSince(start_of_day)   as Number).floatValue != null)
    {
        val rawwater_Heute = (rawwater.deltaSince(start_of_day)   as Number).floatValue
        rawwaterTag.postUpdate(rawwater_Heute)
    }
    if ((rawwater.deltaSince(start_of_week)  as Number).floatValue != null)
    {
        val rawwater_Woche = (rawwater.deltaSince(start_of_week)  as Number).floatValue
        rawwaterWoche.postUpdate(rawwater_Woche)
    }
    if ((rawwater.deltaSince(start_of_month) as Number).floatValue != null)
    {    
        val rawwater_Monat = (rawwater.deltaSince(start_of_month) as Number).floatValue
        rawwaterMonat.postUpdate(rawwater_Monat)
    }
    if ((rawwater.deltaSince(start_of_year)  as Number).floatValue != null)
    {
        val rawwater_Jahr  = (rawwater.deltaSince(start_of_year)  as Number).floatValue
        rawwaterJahr.postUpdate(rawwater_Jahr)
    }

end

Shortly after the start of each new day, I get the following error:

Script execution of rule with UID ‘judo-4’ failed: cannot invoke method public abstract float java.lang.Number.floatValue() on null in judo

What’s the problem here?
This problem doesn’t occur the rest of the day—only shortly after midnight.

Shortly after start of the day is the delta null.. No data.. I have something similar with daily kwh

The problem is the order in your check. deltaSince returns null shortly after midnight, because no persisted value exists yet for the new day. Your if condition already calls .floatValue on that null, so the error is thrown before the check can do anything. Check the result for null first, then cast:

rule "Wasserverbrauch Tag Woche Monat Jahr"
when
    Item rawwater changed
then
    val ZonedDateTime zdt            = ZonedDateTime.now()
    val ZonedDateTime start_of_day   = zdt.with(LocalTime.MIDNIGHT)
    val ZonedDateTime start_of_week  = start_of_day.minusDays(start_of_day.getDayOfWeek.getValue - 1)
    val ZonedDateTime start_of_month = start_of_day.withDayOfMonth(1)
    val ZonedDateTime start_of_year  = start_of_day.withDayOfYear(1)

    val dTag   = rawwater.deltaSince(start_of_day)
    val dWoche = rawwater.deltaSince(start_of_week)
    val dMonat = rawwater.deltaSince(start_of_month)
    val dJahr  = rawwater.deltaSince(start_of_year)

    if (dTag !== null)   rawwaterTag.postUpdate((dTag as Number).floatValue)
    if (dWoche !== null) rawwaterWoche.postUpdate((dWoche as Number).floatValue)
    if (dMonat !== null) rawwaterMonat.postUpdate((dMonat as Number).floatValue)
    if (dJahr !== null)  rawwaterJahr.postUpdate((dJahr as Number).floatValue)
end

Note: until the first value of the new day is persisted, the daily item keeps showing the previous day’s value. If you prefer 0 in that window, add an else rawwaterTag.postUpdate(0) branch.