Null pointer exception related to rule

Hello,

Can someone point out the (probably obvious) problem in this rule that is throwing errors in my logs?

rule "rainInLast24Hours"
	when 
		Item LaCrosse_Rain changed or
		Time cron "0 0/5 * * * ?"
	then
		var Number rainLast24Hours = LaCrosse_Rain.historicState(now.minusHours(24), "rrd4j").state as DecimalType
		var Number rainNow1 = LaCrosse_Rain.state as DecimalType
		var Number rainInLast24Hours = rainNow1 - rainLast24Hours
		postUpdate(Rain_last_24h, rainInLast24Hours)
end

I get the following in logs:

[ERROR] [.o.m.r.i.engine.ExecuteRuleJob] - Error during the execution of rule rainInLast24Hours

java.lang.NullPointerException: cannot invoke method public abstract org.openhab.core.types.State org.openhab.core.persistence.HistoricItem.getState() on null

Thanks in advance!

Rain_last_24h item needs to be defined before you post and update to it.
But just reread your post. Looks like the error is when you get the historic state.

I have the following in my items file:

Number Rain_last_24h "Rain in last 24h [%.1f ml]" <rain> (gOutside)

I am not sure this is your problem, but since your rule can be triggered by a cron expression, there is a chance that the LaCrosse_Rain item is undefined/uninitialized at the time you are trying to access it (LaCrosse_Rain.historicState(), LaCrosse_Rain.state, etc.).

Such a situation would potentially throw all kinds of errors. Believe me, I’ve been bitten by this many times myself, :smile:

You should probably have a check for Uninitialized state on the LaCrosse_Rain item in your rule.

Apart from that, I have also had some problems with .historicState() returning null - especially using rrd4j. Have you configured an EveryMinute strategy for the LaCrosse_Rain item in your rrd4j. persist file?