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:
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
But show the error at Ignition:
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
When run on the gateway (ie, from a tag event/change script), system.db.runNamedQuery requires that you specify the project name that contains the named query as the first argument.
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:
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())
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
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.