Random intermittent null error

Hello, i have the following error:

2015-10-08 00:00:00.978 [ERROR] [.o.m.r.i.engine.ExecuteRuleJob] - Error during the execution of rule Clear daily consumption
java.lang.NullPointerException: cannot invoke method public abstract org.openhab.core.types.State org.openhab.core.persistence.HistoricItem.getState() on null

that runs on this rule

//////////////////////////////////////////////////////////////////////////////////////////////////
rule “Clear daily consumption”
//////////////////////////////////////////////////////////////////////////////////////////////////
when
Time cron “0 0 0 * * ?” // every day
then
sendTweet("Day_total= " +CumulativeEnergyConsumption.state)
postUpdate(DailyEnergyTotal, (CumulativeEnergyConsumption.state as DecimalType - CumulativeEnergyConsumption.historicState(now.minusDays(1)).state as DecimalType))
End

It seems that most nights it runs as i expect it, but then sometimes it fails, and it can fail for several nights in a row.
The tweet gets sent every night without fail

So my questions :smiley:
-I really don’t think i fully understand the concept of null in java (noob alert) can anyone offer a small explaination

-Why do i get this situation sometimes and not others, and what can i do about it?

Thanks for any help or suggestions

CumulativeEnergyConsumption.historicState(now.minusDays(1)) is sometimes failing to return the HistoricState from yesterday, which could either be a bug or config issue with the persistence layer. In Java or the rule language, null means there is no value for the expression or variable. Can you chart or otherwise query the underlying database to see what values are stored? Which persistence bundle are you using?

Hi @watou and thanks for your response, i am using rrd4j, i’ve tried to graph it and there doesn’t seem to be any issues. its a nice smooth plot with no missing blocks. maybe i will try outputting the vales somewhere before calculating

I hope someone else takes a look to see if this should have an issue opened for it in github!

Are you using “restoreOnStartup” in your persistence? The reason I ask is that I’ve seen some cases where at midnight some of my items that don’t get restored (e.g. String items which rrd4j doesn’t support) get reset to undefined.

To see if that is the case you could maybe the cron to a few minutes before or after midnight. You could be in a situation where things are being reset, refreshed, etc at the same time your rule is trying to run. That would explain the intermittentness of the error.

It is very weird that the error is intermittent and that makes me think that scheduling it at midnight could be the cause. At least it is easy to test.

Thanks @rlkoshak, i will test with “0 57 23 1/1 * ? *” to see if it is more reliable.