# NullPointerException at NamedQuey for Insert

**URL:** <https://forum.inductiveautomation.com/t/nullpointerexception-at-namedquey-for-insert/21197>\
**Category:** Ignition\
**Created:** [December 3, 2018, 4:38pm UTC](https://forum.inductiveautomation.com/t/nullpointerexception-at-namedquey-for-insert/21197 "2018-12-03T16:38:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![AGDC04](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/agdc04/32/6366_2.png) [@AGDC04](https://forum.inductiveautomation.com/u/AGDC04)\
**Post date:** [December 3, 2018, 4:38pm UTC](https://forum.inductiveautomation.com/t/nullpointerexception-at-namedquey-for-insert/21197/1 "2018-12-03T16:38:38Z")

</div>

Hello,

I’m trying to insert data at a SQL, using a NamedQuery, but showing NullPointer, but can’t see the error:

This is the code at ValueChange script:

```auto
if (currentValue.value != previousValue.value and currentValue.value == 6):
		line = system.tag.getTagValue("[.]WorkGroup")
		station = system.tag.getTagValue("[.]Station Number")
		startWork = system.date.parse(system.tag.getTagValue("[.]Start Work"))
		endWork = system.date.parse(system.date.now())
		serialNumber = system.tag.getTagValue("[.]Serial Number")
		totalTakt = system.tag.getTagValue("[.]Counter Station")
		amesUser = "NA"
		taktLine = system.tag.getTagValue("[.]Line Takt Goal")
		params = {"LINE":line,"STATION":station,"START_WORK":startWork,"END_WORK":endWork,"SERIAL_NUMBER":serialNumber,"TOTAL_TAKT":totalTakt,"AMES_USER":amesUser,"TAKT_LINE":taktLine}
		result = system.db.runNamedQuery("Insert Takt History", params)
		counter = system.tag.getTagValue("[.]Station Actual")
		counter = counter + 1
		system.tag.writeToTag("[.]Station Actual", counter)
		
	if (currentValue.value != previousValue.value and currentValue.value == 2):
		system.tag.writeToTag("[.]Start Work", system.date.now())

```

This is the print of data at UDT

![](https://cdck-file-uploads-us1.s3.dualstack.us-west-2.amazonaws.com/inductiveautomation/original/2X/7/7caeccd84123374922393390602fdb80fbfe834c.png)

But show the error at Ignition:

```auto
com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last): File "", line 20, in valueChanged at com.inductiveautomation.ignition.gateway.script.GatewayDBUtilities.runNamedQuery(GatewayDBUtilities.java:354) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) java.lang.NullPointerException: java.lang.NullPointerException

at org.python.core.Py.JavaError(Py.java:495)

at org.python.core.Py.JavaError(Py.java:488)

at org.python.core.PyReflectedFunction. __call__ (PyReflectedFunction.java:188)

at com.inductiveautomation.ignition.common.script.ScriptManager$ReflectedInstanceFunction. __call__ (ScriptManager.java:431)

at org.python.core.PyObject. __call__ (PyObject.java:404)

at org.python.core.PyObject. __call__ (PyObject.java:408)

at org.python.pycode._pyx58.valueChanged$1(:26)

at org.python.pycode._pyx58.call_function()

at org.python.core.PyTableCode.call(PyTableCode.java:165)

at org.python.core.PyBaseCode.call(PyBaseCode.java:301)

at org.python.core.PyFunction.function ___call__ (PyFunction.java:376)

at org.python.core.PyFunction. __call__ (PyFunction.java:371)

at org.python.core.PyFunction. __call__ (PyFunction.java:361)

at org.python.core.PyFunction. __call__ (PyFunction.java:356)

at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:649)

at com.inductiveautomation.ignition.common.sqltags.scripts.TagScriptManager$FunctionInvokerImpl.run(TagScriptManager.java:493)

at com.inductiveautomation.ignition.common.sqltags.scripts.AbstractTagScript.invoke(AbstractTagScript.java:33)

at com.inductiveautomation.ignition.common.sqltags.scripts.TagScriptManager$Task.invoke(TagScriptManager.java:442)

at com.inductiveautomation.ignition.common.sqltags.scripts.TagScriptManager$TagScriptDispatcher.run(TagScriptManager.java:405)

at com.inductiveautomation.ignition.common.execution.impl.BasicExecutionEngine$ThrowableCatchingRunnable.run(BasicExecutionEngine.java:518)

at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)

at java.util.concurrent.FutureTask.run(FutureTask.java:266)

at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)

at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)

at java.lang.Thread.run(Thread.java:748)

Caused by: org.python.core.PyException: Traceback (most recent call last): File "", line 20, in valueChanged at com.inductiveautomation.ignition.gateway.script.GatewayDBUtilities.runNamedQuery(GatewayDBUtilities.java:354) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) java.lang.NullPointerException: java.lang.NullPointerException

... 27 common frames omitted

Caused by: java.lang.NullPointerException: null

at com.inductiveautomation.ignition.gateway.script.GatewayDBUtilities.runNamedQuery(GatewayDBUtilities.java:354)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.python.core.PyReflectedFunction. __call__ (PyReflectedFunction.java:186)

... 24 common frames omitted

```

Could you help with this?

Thanks

---

<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:** [December 3, 2018, 4:50pm UTC](https://forum.inductiveautomation.com/t/nullpointerexception-at-namedquey-for-insert/21197/2 "2018-12-03T16:50:45Z")

</div>

When run on the gateway (ie, from a tag event/change script), [`system.db.runNamedQuery`](https://docs.inductiveautomation.com/display/DOC79/system.db.runNamedQuery) requires that you specify the project name that contains the named query as the first argument.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [December 3, 2018, 4:51pm UTC](https://forum.inductiveautomation.com/t/nullpointerexception-at-namedquey-for-insert/21197/3 "2018-12-03T16:51:31Z")

</div>

I’m not sure what version of Ignition you are using, but I believe you should be using

```auto
system.tag.read(tag).value 

system.tag.write(tag,value)

```

---

<div class="post-metadata">

**Author:** ![AGDC04](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/agdc04/32/6366_2.png) [@AGDC04](https://forum.inductiveautomation.com/u/AGDC04)\
**Post date:** [December 3, 2018, 5:29pm UTC](https://forum.inductiveautomation.com/t/nullpointerexception-at-namedquey-for-insert/21197/4 "2018-12-03T17:29:28Z")

</div>

Updated the code with tag.read. Also the NamedQuery is at local project.  
I not understand about the specify project name, because the script is inside a Tag Change event of UDT:

```auto
	if (currentValue.value != previousValue.value and currentValue.value == 6):
		line = system.tag.read("[.]WorkGroup").value
		station = system.tag.read("[.]Station Number").value
		startWork = system.tag.read("[.]Start Work").value
		endWork = system.date.now()
		serialNumber = system.tag.read("[.]Serial Number").value
		totalTakt = system.tag.read("[.]Counter Station").value
		amesUser = "NA"
		taktLine = system.tag.read("[.]Line Takt Goal").value
		params = {"LINE":line,"STATION":station,"START_WORK":startWork,"END_WORK":endWork,"SERIAL_NUMBER":serialNumber,"TOTAL_TAKT":totalTakt,"AMES_USER":amesUser,"TAKT_LINE":taktLine}
		result = system.db.runNamedQuery("Insert Takt History", params)
		counter = system.tag.read("[.]Station Actual").value
		counter = counter + 1
		system.tag.write("[.]Station Actual", counter)
		
	if (currentValue.value != previousValue.value and currentValue.value == 2):
		system.tag.write("[.]Start Work", system.date.now())

```

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/8/821b5155857031113d26900b6bdd77df05bb16a8.png)

The SQL Table Script

```auto
CREATE TABLE [dbo].[STATION_TAKT_HISTORY2](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[LINE] [nchar](20) NULL,
	[STATION] [nchar](20) NULL,
	[START_WORK] [datetime] NULL,
	[END_WORK] [datetime] NULL,
	[SERIAL_NUMBER] [nchar](17) NULL,
	[TOTAL_TAKT] [real] NULL,
	[AMES_USER] [nchar](10) NULL,
	[TAKT_LINE] [real] NULL
) ON [PRIMARY]
GO

```

Named Query  
 ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/1/1d87e4405e0d6621d026ab1e56434c700ca83100.png)

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/5/551599c6abae1f4fdcb4b39d44a1a458d7efc4d8.png)

Same error at Log:

```auto
com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last): File "", line 12, in valueChanged at com.inductiveautomation.ignition.gateway.script.GatewayDBUtilities.runNamedQuery(GatewayDBUtilities.java:354) at sun.reflect.GeneratedMethodAccessor119.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) java.lang.NullPointerException: java.lang.NullPointerException

at org.python.core.Py.JavaError(Py.java:495)

at org.python.core.Py.JavaError(Py.java:488)

at org.python.core.PyReflectedFunction. __call__ (PyReflectedFunction.java:188)

at com.inductiveautomation.ignition.common.script.ScriptManager$ReflectedInstanceFunction. __call__ (ScriptManager.java:431)

at org.python.core.PyObject. __call__ (PyObject.java:404)

at org.python.core.PyObject. __call__ (PyObject.java:408)

at org.python.pycode._pyx62.valueChanged$1(:18)

at org.python.pycode._pyx62.call_function()

at org.python.core.PyTableCode.call(PyTableCode.java:165)

at org.python.core.PyBaseCode.call(PyBaseCode.java:301)

at org.python.core.PyFunction.function ___call__ (PyFunction.java:376)

at org.python.core.PyFunction. __call__ (PyFunction.java:371)

at org.python.core.PyFunction. __call__ (PyFunction.java:361)

at org.python.core.PyFunction. __call__ (PyFunction.java:356)

at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:649)

at com.inductiveautomation.ignition.common.sqltags.scripts.TagScriptManager$FunctionInvokerImpl.run(TagScriptManager.java:493)

at com.inductiveautomation.ignition.common.sqltags.scripts.AbstractTagScript.invoke(AbstractTagScript.java:33)

at com.inductiveautomation.ignition.common.sqltags.scripts.TagScriptManager$Task.invoke(TagScriptManager.java:442)

at com.inductiveautomation.ignition.common.sqltags.scripts.TagScriptManager$TagScriptDispatcher.run(TagScriptManager.java:405)

at com.inductiveautomation.ignition.common.execution.impl.BasicExecutionEngine$ThrowableCatchingRunnable.run(BasicExecutionEngine.java:518)

at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)

at java.util.concurrent.FutureTask.run(FutureTask.java:266)

at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)

at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)

at java.lang.Thread.run(Thread.java:748)

Caused by: org.python.core.PyException: Traceback (most recent call last): File "", line 12, in valueChanged at com.inductiveautomation.ignition.gateway.script.GatewayDBUtilities.runNamedQuery(GatewayDBUtilities.java:354) at sun.reflect.GeneratedMethodAccessor119.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) java.lang.NullPointerException: java.lang.NullPointerException

... 27 common frames omitted

Caused by: java.lang.NullPointerException: null

at com.inductiveautomation.ignition.gateway.script.GatewayDBUtilities.runNamedQuery(GatewayDBUtilities.java:354)

at sun.reflect.GeneratedMethodAccessor119.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.python.core.PyReflectedFunction. __call__ (PyReflectedFunction.java:186)

... 24 common frames omitted

```

---

<div class="post-metadata">

**Author:** ![AGDC04](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/agdc04/32/6366_2.png) [@AGDC04](https://forum.inductiveautomation.com/u/AGDC04)\
**Post date:** [December 3, 2018, 5:30pm UTC](https://forum.inductiveautomation.com/t/nullpointerexception-at-namedquey-for-insert/21197/5 "2018-12-03T17:30:34Z")

</div>

Version of Ignition 7.9.10

---

<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:** [December 3, 2018, 5:46pm UTC](https://forum.inductiveautomation.com/t/nullpointerexception-at-namedquey-for-insert/21197/6 "2018-12-03T17:46:01Z")

</div>

> [@AGDC04](#):
>
> I not understand about the specify project name, because the script is inside a Tag Change event of UDT:

Right - like I said, _when running from a tag event script_, named queries require you to specify a project name.  
Tags are _always_ executing - they reside on the gateway itself, not inside any particular project, so your UDT, its member tags, and all of your other tags are _unaware_ of your "local" project. If you create a new project and open the tag browser, you will see the exact same tags. You **must** specify a project name when calling a named query from the global scope, to tell the _gateway_ which project to find the named query in.

---

<div class="post-metadata">

**Author:** ![AGDC04](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/agdc04/32/6366_2.png) [@AGDC04](https://forum.inductiveautomation.com/u/AGDC04)\
**Post date:** [December 3, 2018, 6:01pm UTC](https://forum.inductiveautomation.com/t/nullpointerexception-at-namedquey-for-insert/21197/7 "2018-12-03T18:01:37Z")

</div>

Right, now I catch you.

Changed the script to:

```auto
		result = system.db.runNamedQuery("JDHMI_BZ","Insert Takt History", params)

```

Works fine now. Thanks for help and clarify.
