Rule throws null exception for non null value


(Pascal Turbo) #1

Hi There,

this problem is annoying. Here a snippet from my code:

logInfo("Venting","Current Venting Duration: " + venting_duration.toString)
  if (current_vent_timer < venting_duration) {
    var Number new_venting_duration = Math::floor(venting_duration)
  }

The annoying thing is, that logging of the venting_duration_value returns a value but calling Math::floor() says, that value is null. How is this even possible :frowning:

2018-07-11 18:35:37.476 [INFO ] [g.openhab.model.script.Venting] - Current Venting Duration: 2.88
2018-07-11 18:35:37.481 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'Venting GF Bath - Set Venting Duration': Could not invoke method: java.lang.Math.floor(double) on instance: null

(Matthias) #2

How have you defined venting_duration? Is it of type Double or Number?


(Rich Koshak) #3

If venting_duration is a Number or DecimalType then I would expect this error. Math::floor requires a primitive.

If venting_duration were a primitive (e.g. float) then I would expect this error on the venting_duration.toString line because primitives are not Objects and therefore do not have a toString method.

I’m going to guess this will work, given the lack of knowing what venting_duration actually is:

Math::floor(venting_duration.floatValue)

(Pascal Turbo) #4

Thanks @rlkoshak. floatValue did it’s job.

But I think, this is a bug. I understand your explanation why it throws this exception, but a framework shouldn’t expect from its users how it works on the underground. So I would expect a “not a float value”. Otherwise it could also write “an error occured”. This would help me in the same way the null value messages helps me - absolutely not.


(Rich Koshak) #5

If you are working with Rules DSL classes and Objects then you don’t need to know about this stuff. If you want to use Java classes and Objects, and Math is a Java class, then you may have to know about this stuff. It’s a limitation of making available libraries from one language (Java) in another language (Rules DSL). It can’t be helped except to either forbid such access (in which case there wouldn’t be any way for you to get the floor of a floating point number).

It is not ideal, but it can’t be helped.

In Rules DSL, when you see Could not invoke method: some method(argument) on instance: null it always means you have a type problem. In this case, you were trying to pass a Number object to a method that expects a double primitive.