# onMessageReceived local variable 'payload' referenced before assignment

**URL:** <https://forum.inductiveautomation.com/t/onmessagereceived-local-variable-payload-referenced-before-assignment/37371>\
**Category:** Ignition\
**Created:** [August 4, 2020, 4:55pm UTC](https://forum.inductiveautomation.com/t/onmessagereceived-local-variable-payload-referenced-before-assignment/37371 "2020-08-04T16:55:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonathan](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@Jonathan](https://forum.inductiveautomation.com/u/Jonathan)\
**Post date:** [August 4, 2020, 4:55pm UTC](https://forum.inductiveautomation.com/t/onmessagereceived-local-variable-payload-referenced-before-assignment/37371/1 "2020-08-04T16:55:57Z")

</div>

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

```auto
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

```

```auto
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

```

---

<div class="post-metadata">

**Author:** ![Jonathan](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@Jonathan](https://forum.inductiveautomation.com/u/Jonathan)\
**Post date:** [August 4, 2020, 5:05pm UTC](https://forum.inductiveautomation.com/t/onmessagereceived-local-variable-payload-referenced-before-assignment/37371/2 "2020-08-04T17:05:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 4, 2020, 5:57pm UTC](https://forum.inductiveautomation.com/t/onmessagereceived-local-variable-payload-referenced-before-assignment/37371/3 "2020-08-04T17:57:39Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Jonathan](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@Jonathan](https://forum.inductiveautomation.com/u/Jonathan)\
**Post date:** [August 4, 2020, 6:25pm UTC](https://forum.inductiveautomation.com/t/onmessagereceived-local-variable-payload-referenced-before-assignment/37371/4 "2020-08-04T18:25:01Z")

</div>

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

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/5/4/5426ac08044b0b8de435e286983eb4f32fe90616.png)

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

```auto
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
