[SOLVED] sendCommand on instance: null trouble

Hello,

I get an error that I don’t understand how to solve.
My code is based on https://github.com/openhab/openhab/wiki/Controlling-openHAB-with-your-voice .

Part of the rule that gives me the error.

    if (itemType!=null && (roomItemPart!=null || command.contains("alle")) && newState!=null) {
        logInfo("rule", "60 sending "+newState+" to "+itemType+"_"+roomItemPart+itemSubType)
        if (command.contains("alle")) {
			logInfo("rule", "62 command contains alle")
            if (roomItemPart==null)
                roomItemPart=""
            val String itemName = itemType+"_"+roomItemPart+itemSubType
            val State finalState = newState
            logInfo("rules","70 searching for "+itemName)
			gVC?.allMembers.filter(s | s.name.contains(itemName) && s.acceptedDataTypes.contains(finalState.class)).forEach[item| 
                logInfo("rules","72 item "+item.name+" found. Sending command "+finalState)
				sendCommand(item, finalState.toString)
            ] 
			  
        } else {
			logInfo("rules", "Command om te zenden "+itemType+"_"+roomItemPart+itemSubType+", "+newState)
            sendCommand(itemType+"_"+roomItemPart+itemSubType, newState.toString)
        }
    }

When I specify one light it works good. But when using the keyword “alle” makes it crash.
Gets met the following output:

2016-03-02 00:44:08.126 [INFO ] [org.openhab.model.script.rule ] - 62 command contains alle
2016-03-02 00:44:08.138 [INFO ] [org.openhab.model.script.rules] - 70 searching for Lamp_BG_Woonkamer
2016-03-02 00:44:08.177 [INFO ] [org.openhab.model.script.rules] - 72 item Lamp_BG_Woonkamer_Bank found. Sending command ON
2016-03-02 00:44:08.178 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'Voice control': Could not invoke method: org.openhab.model.script.actions.BusEvent.sendCommand(org.openhab.core.items.Item,java.lang.Number) on instance: null

I tried to manually input an item to switch and that works fine. So there must be something wrong with the item part in “sendCommand(item, finalState.toString)”. But I don’t understand what’s wrong with it. The log statement above it gives me an output, but the sendCommand statement complaints that the instance is null if I’m understanding correctly.

Is this some stupid mistake I am not seeing?

Two things you can try:

First:

item.sendCommand(finalState)

Second:

sendCommand(item.name, finalState.toString)

Sadly this gives me the same error:

    if (itemType!=null && (roomItemPart!=null || command.contains("alle")) && newState!=null) {
        logInfo("rule", "60 sending "+newState+" to "+itemType+"_"+roomItemPart+itemSubType)
        if (command.contains("alle")) {
			logInfo("rule", "62 command contains alle")
            if (roomItemPart==null)
                roomItemPart=""
            val String itemName = itemType+"_"+roomItemPart+itemSubType
            val State finalState = newState
            logInfo("rules","70 searching for "+itemName)
			gVC?.allMembers.filter(s | s.name.contains(itemName) && s.acceptedDataTypes.contains(finalState.class)).forEach[item| 
                logInfo("rules","72 item "+item.name+" found. Sending command "+finalState)
				item.sendCommand(finalState)
            ] 
			  
        } else {
			logInfo("rules", "77 Command om te zenden "+itemType+"_"+roomItemPart+itemSubType+", "+newState)
            sendCommand(itemType+"_"+roomItemPart+itemSubType, newState.toString)
        }
    }

This is the output:

2016-03-02 19:35:11.203 [INFO ] [org.openhab.model.script.rule ] - 60 sending ON to Lamp_BG_Woonkamer
2016-03-02 19:35:11.206 [INFO ] [org.openhab.model.script.rule ] - 62 command contains alle
2016-03-02 19:35:11.216 [INFO ] [org.openhab.model.script.rules] - 70 searching for Lamp_BG_Woonkamer
2016-03-02 19:35:11.249 [INFO ] [org.openhab.model.script.rules] - 72 item Lamp_BG_Woonkamer_Plafond found. Sending command ON
2016-03-02 19:35:11.250 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'Voice control': Could not invoke method: org.openhab.model.script.actions.BusEvent.sendCommand(org.openhab.core.items.Item,java.lang.Number) on instance: null

Same:

    if (itemType!=null && (roomItemPart!=null || command.contains("alle")) && newState!=null) {
        logInfo("rule", "60 sending "+newState+" to "+itemType+"_"+roomItemPart+itemSubType)
        if (command.contains("alle")) {
			logInfo("rule", "62 command contains alle")
            if (roomItemPart==null)
                roomItemPart=""
            val String itemName = itemType+"_"+roomItemPart+itemSubType
            val State finalState = newState
            logInfo("rules","70 searching for "+itemName)
			gVC?.allMembers.filter(s | s.name.contains(itemName) && s.acceptedDataTypes.contains(finalState.class)).forEach[item| 
                logInfo("rules","72 item "+item.name+" found. Sending command "+finalState)
				sendCommand(item.name, finalState.toString)
            ] 
			  
        } else {
			logInfo("rules", "77 Command om te zenden "+itemType+"_"+roomItemPart+itemSubType+", "+newState)
            sendCommand(itemType+"_"+roomItemPart+itemSubType, newState.toString)
        }
    }

Same error.

2016-03-02 19:41:19.903 [INFO ] [org.openhab.model.script.rule ] - 60 sending OFF to Lamp_BG_Woonkamer
2016-03-02 19:41:19.905 [INFO ] [org.openhab.model.script.rule ] - 62 command contains alle
2016-03-02 19:41:19.919 [INFO ] [org.openhab.model.script.rules] - 70 searching for Lamp_BG_Woonkamer
2016-03-02 19:41:19.969 [INFO ] [org.openhab.model.script.rules] - 72 item Lamp_BG_Woonkamer_Plafond found. Sending command OFF
2016-03-02 19:41:19.969 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'Voice control': Could not invoke method: org.openhab.model.script.actions.BusEvent.sendCommand(org.openhab.core.items.Item,java.lang.Number) on instance: null

When I replace the item variable to a written Item name it does not give me errors. What I tried:

    if (itemType!=null && (roomItemPart!=null || command.contains("alle")) && newState!=null) {
        logInfo("rule", "60 sending "+newState+" to "+itemType+"_"+roomItemPart+itemSubType)
        if (command.contains("alle")) {
			logInfo("rule", "62 command contains alle")
            if (roomItemPart==null)
                roomItemPart=""
            val String itemName = itemType+"_"+roomItemPart+itemSubType
            val State finalState = newState
            logInfo("rules","70 searching for "+itemName)
			gVC?.allMembers.filter(s | s.name.contains(itemName) && s.acceptedDataTypes.contains(finalState.class)).forEach[item| 
                logInfo("rules","72 item "+item.name+" found. Sending command "+finalState)
				sendCommand(Lamp_BG_Woonkamer_Plafond, finalState.toString)
            ] 
			  
        } else {
			logInfo("rules", "77 Command om te zenden "+itemType+"_"+roomItemPart+itemSubType+", "+newState)
            sendCommand(itemType+"_"+roomItemPart+itemSubType, newState.toString)
        }
    }

This runs fine and shows in the log below that the .filter method works fine. Only now it switches a hard coded Item.

2016-03-02 19:31:59.548 [INFO ] [org.openhab.model.script.rule ] - 60 sending OFF to Lamp_BG_Woonkamer
2016-03-02 19:31:59.550 [INFO ] [org.openhab.model.script.rule ] - 62 command contains alle
2016-03-02 19:31:59.559 [INFO ] [org.openhab.model.script.rules] - 70 searching for Lamp_BG_Woonkamer
2016-03-02 19:31:59.595 [INFO ] [org.openhab.model.script.rules] - 72 item Lamp_BG_Woonkamer_Plafond found. Sending command OFF
2016-03-02 19:31:59.601 [INFO ] [org.openhab.model.script.rules] - 72 item Lamp_BG_Woonkamer_Bank found. Sending command OFF

Above gives me the impression that something funky is going on with the variable “item” that is produced by the .filter method. What can be the problem?
Am I missing some import maybe?

Below states what is imported at the beginning of the file.

import org.openhab.core.types.*
import org.openhab.model.script.actions.*
import org.openhab.core.library.types.*
import org.openhab.core.persistence.*

The error message “Could not invoke method: […].sendCommand(org.openhab.core.items.Item,java.lang.Number) on instance: null” makes me think that the sendCommand method sees the item variable as null. But the logInfo statement can output the item variable. How can this be?
I don’t get it, other than that I am missing some import. Can it be confirmed that I have imported everything needed?

(Sorry for the lengthy post.)

Perhaps the filter isn’t actually finding the Item. But that doesn’t make sense because you are logging the item.name.

Unfortunately the rules DSL throws a null exception any time it cannot interpret, transform, cast, or otherwise not understand the data. In other words, a null exception does not necessarily mean the item is null.

What type of Items are in gVC? Can every Item in the group support OnOffType(i.e. Switch, Dimmer, Color)? You could get this exception if you try to send an OnOffType to, for example, a Number Item or a Contact Item.

Imports are not your problem.

1 Like

I fixed it!
The code that works for me:

    if (itemType!=null && (roomItemPart!=null || command.contains("alle")) && newState!=null) {
        logInfo("rule", "64 sending "+newState+" to "+itemType+"_"+roomItemPart+itemSubType)
        if (command.contains("alle")) {
			logInfo("rule", "66 command contains alle")
            if (roomItemPart==null)
                roomItemPart=""
            val String itemName = itemType+"_"+roomItemPart+itemSubType
            val State finalState = newState
            logInfo("rules","71 searching for "+itemName)
			gVC?.allMembers.filter(s | s.name.contains(itemName) && s.acceptedDataTypes.contains(finalState.class)).forEach[item| 
                logInfo("rules","73 item "+item.name+" found. Sending command "+finalState)
				var String itemvar = item.name.toString
				logInfo("rules","75 var itemvar: "+itemvar)
				sendCommand(itemvar, finalState.toString)
            ] 
			  
        } else {
			logInfo("rules", "77 Command om te zenden "+itemType+"_"+roomItemPart+itemSubType+", "+newState)
            sendCommand(itemType+"_"+roomItemPart+itemSubType, newState.toString)
        }
    }

So I take an extra step, var String itemvar = item.name.toString, and use itemvar in, sendCommand(itemvar, finalState.toString). Now it runs fine!

These are of type Dimmer, so that does work.

@rlkoshak Thank you for your time!

1 Like

Hi,
It didn’t work for me.
Instead i deleted the items file and create it again (The same content). This solved the error,