onMessageReceived local variable 'payload' referenced before assignment

I am running version 8.0.15 and on my message handler I am receiving the following message.

def onMessageReceived(self, payload):
	"""
	This method will be called when a message with the matching type code
	arrives at this component.

	Arguments:
		self: A reference to this component
		payload: The data object sent along with the message
	"""
	self.custom.jobPath = payload
com.inductiveautomation.ignition.common.script.JythonExecException
Traceback (most recent call last):
  File "<function:onMessageReceived>", line 2, in onMessageReceived
UnboundLocalError: local variable 'payload' referenced before assignment

	at org.python.core.Py.UnboundLocalError(Py.java:295)
	at org.python.core.PyFrame.getlocal(PyFrame.java:240)
	at org.python.pycode._pyx689.onMessageReceived$1(<function:onMessageReceived>:2)
	at org.python.pycode._pyx689.call_function(<function:onMessageReceived>)
	at org.python.core.PyTableCode.call(PyTableCode.java:171)
	at org.python.core.PyBaseCode.call(PyBaseCode.java:308)
	at org.python.core.PyFunction.function___call__(PyFunction.java:471)
	at org.python.core.PyFunction.__call__(PyFunction.java:466)
	at org.python.core.PyFunction.__call__(PyFunction.java:461)
	at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:822)
	at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:806)
	at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$TrackingProjectScriptManager.runFunction(ProjectScriptLifecycle.java:687)
	at com.inductiveautomation.ignition.common.script.ScriptManager$ScriptFunctionImpl.invoke(ScriptManager.java:965)
	at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$AutoRecompilingScriptFunction.invoke(ProjectScriptLifecycle.java:752)
	at com.inductiveautomation.perspective.gateway.script.ScriptFunctionHelper.invoke(ScriptFunctionHelper.java:101)
	at com.inductiveautomation.perspective.gateway.model.MessageHandlerCollection$MessageHandlerImpl$1.lambda$invoke$0(MessageHandlerCollection.java:80)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
	at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.base/java.lang.Thread.run(Unknown Source)
Caused by: org.python.core.PyException
Traceback (most recent call last):
  File "<function:onMessageReceived>", line 2, in onMessageReceived
UnboundLocalError: local variable 'payload' referenced before assignment

	... 21 more

Ignition v8.0.15 (b2020072213)
Java: Azul Systems, Inc. 11.0.6

I think i solved the issue. It looks like the message handler does not like being sent None for the payload. I changed my code where the message is being sent to send β€œβ€ if the value is None.

1 Like

Hmm, so you were specifically calling it as system.perspective.sendMessage("handler", None)? What happens if you just don’t specify a payload argument in the call?

I have a custom property called jobPath that is bound to the this.props.selection[0] of the tree component.

image

This custom property jobPath then has a change script on it that send the current value as the payload of the message.

def valueChanged(self, previousValue, currentValue, origin):
	"""
	This function will be called when the value of the property changes.

	Arguments:
		self: A reference to the component that is invoking this function.
		previousValue: The previous value, as a qualified value object.
		currentValue: The new value, as a qualified value object.
		origin: The origin of the property value. Possible origin values include
		        Browser, Binding, BindingWriteback, Script, Delegate, Session, Project
	"""
	jobPath = "" if currentValue.value == None else currentValue.value
	
	system.perspective.sendMessage(messageType="jobTreeItemSelected", payload=jobPath)

I was not having this issue on 8.0.12