ScriptActionHandler: Could not invoke method: ... on instance: null

I am looking help from other OH developers concerning ‘ScriptActionHandler: Could not invoke method: … on instance: null’ exception that I sometimes get from my Growatt binding’s implementation of ThingActions. Specifically what could be causing OH Core to think that ‘instance’ (instance of what?) is null?

2024-07-03 00:00:00.265 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID {snip} failed: An error occurred during the script execution: Could not invoke method: {snip} on instance: null in 24g

In Rules DSL, which I presume this is what’s being used for this rule, that sort of exception doesn’t necessarily mean that there is a variable that is null. The type system in Rules DSL tries all sorts of things to try to coerce the types of Objects into something compatible with the way those Objects are being used. However, when it fails, it will throw an exception like this.

For example, in some cases myIntVariable + " is a number" will sometimes throw a null error because it tries to coerce the " is a number" String to become a Number and fails. However, "This is a number " + myIntVariable would work just fine because when Rules DSL tries to coerce myIntVariable to become a String it knows how to do it.

Over the years the error and type checking have become better so you encounter this sort of ambiguous error less and less frequently and see meaningful errors instead. But “Could not invoke method:… on instance: null” is the classic “Rules DSL failed to convert the arguments to the type it needs” error message.

In short, it’s not a problem with the binding, it’s a problem in the rule and specifically the types of the variables passed in the call to the action.

You’ve snipped out the important bits and haven’t shown the actual line of code so :man_shrugging: as to what the actual problem is. Usually, calling toString on arguments will work unless it’s a number in which case calling floatValue often works.

1 Like

@rlkoshak many thanks for the very helpful explanation. The actual problem was indeed as described in your response – specifically as follows…

val minSOC = 10.0 // creates a float ?? or a double ??
val Integer stopSoc = Math.round(minSOC)

call thing action with @Nullable Integer stopSoc as argument
=> throws exception "Could not invoke method: .. on instance: null"

I detected the error by trying to do the float to integer cast explicitly in the rule rather than in the action method call…

val Integer stopSoc = Integer.valueOf(Math.round(minSOC))
=> throws exception "Could not invoke method: java.lang.Integer.valueOf(java.lang.String) on instance: null"

It is a pity that the log message doesn’t give a better clue. I am wondering if I can create a PR to fix it. Although digging through the stack trace, it seems that the message originates in Java library code outside of OH, so that may be tricky…

1 Like

Better error messages are always a good thing. But in this case, as you guess, the error is occurring deep within the Xtend library and since Math is part of Java instead of openHAB, your opportunities to detect and provide a more meaningful write may be limited.

I know that better error messages now occur for timers, log statements and a lot of other OH calls but I don’t know how they do it. Looking at createTimer might give some clues.