Using this code to load an image from BLOB in the database into the icon field of a label.
When the code is attached to a button, it works fine, but I need the code to load the image as soon as the popup window is opened. I’m trying to add the code when the window is activated / opened, but get a null exception:
The Code in WindowActivated/WindowOpened
from javax.swing import ImageIcon
from java.io import ByteArrayInputStream
from javax.imageio import ImageIO
from java.awt import Image
from java.net import URL
from java.io import File
image = system.db.runScalarPrepQuery("SELECT image FROM Products where id=15757")
bais = ByteArrayInputStream(image)
bImageFromConvert = ImageIO.read(bais)
lbl3 = event.source.parent.getComponent('Label 29')
boundWidth = lbl3.width
boundHeight = lbl3.height
originalWidth = bImageFromConvert.width
originalHeight = bImageFromConvert.height
newWidth = originalWidth
newHeight = originalHeight
if originalWidth > boundWidth:
newWidth = boundWidth
newHeight = (newWidth * originalHeight) / originalWidth
if newHeight > boundHeight:
newHeight = boundHeight
newWidth = (newHeight * originalWidth) / originalHeight
scaledImage = bImageFromConvert.getScaledInstance(newWidth,newHeight,Image.SCALE_SMOOTH)
lbl3.setIcon(ImageIcon(scaledImage))
The Error in designer:
Traceback (most recent call last):
File "<event:internalFrameOpened>", line 10, in <module>
at org.python.core.PyJavaType.wrapJavaObject(PyJavaType.java:90)
at com.inductiveautomation.factorypmi.application.script.PyComponentWrapper.<init>(PyComponentWrapper.java:57)
at com.inductiveautomation.factorypmi.application.script.PyComponentWrapper.__findattr_ex__(PyComponentWrapper.java:94)
at org.python.core.PyObject.__getattr__(PyObject.java:1025)
at org.python.pycode._pyx29.f$0(<event:internalFrameOpened>:24)
at org.python.pycode._pyx29.call_function(<event:internalFrameOpened>)
at org.python.core.PyTableCode.call(PyTableCode.java:171)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.Py.runCode(Py.java:1614)
at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:770)
at com.inductiveautomation.factorypmi.application.binding.action.ActionAdapter.runActions(ActionAdapter.java:200)
at com.inductiveautomation.factorypmi.application.binding.action.ActionAdapter.invoke(ActionAdapter.java:291)
at com.inductiveautomation.factorypmi.application.binding.action.RelayInvocationHandler.invoke(RelayInvocationHandler.java:57)
at com.sun.proxy.$Proxy60.internalFrameOpened(Unknown Source)
at java.desktop/javax.swing.JInternalFrame.fireInternalFrameEvent(Unknown Source)
at java.desktop/javax.swing.JInternalFrame.show(Unknown Source)
at java.desktop/java.awt.Component.show(Unknown Source)
at java.desktop/java.awt.Component.setVisible(Unknown Source)
at java.desktop/javax.swing.JComponent.setVisible(Unknown Source)
at com.inductiveautomation.factorypmi.designer.workspace.WindowWorkspace$DesigntimeWindowOpener.openWindow(WindowWorkspace.java:3514)
at com.inductiveautomation.factorypmi.application.VisionDesktop.openWindow(VisionDesktop.java:197)
at com.inductiveautomation.factorypmi.application.script.builtin.NavUtilities.lambda$openWindowImpl$5(NavUtilities.java:393)
at com.inductiveautomation.factorypmi.application.script.builtin.WindowUtilities.doGUIAction(WindowUtilities.java:808)
at com.inductiveautomation.factorypmi.application.script.builtin.NavUtilities.openWindowImpl(NavUtilities.java:336)
at com.inductiveautomation.factorypmi.application.script.builtin.NavUtilities.openWindow(NavUtilities.java:326)
at com.inductiveautomation.factorypmi.application.script.builtin.NavUtilitiesDispatcher.openWindow(NavUtilitiesDispatcher.java:143)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
java.lang.NullPointerException: java.lang.NullPointerException
at org.python.core.Py.JavaError(Py.java:552)
at org.python.core.PyTableCode.call(PyTableCode.java:180)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.Py.runCode(Py.java:1614)
at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:770)
at com.inductiveautomation.factorypmi.application.binding.action.ActionAdapter.runActions(ActionAdapter.java:200)
at com.inductiveautomation.factorypmi.application.binding.action.ActionAdapter.invoke(ActionAdapter.java:291)
at com.inductiveautomation.factorypmi.application.binding.action.RelayInvocationHandler.invoke(RelayInvocationHandler.java:57)
at com.sun.proxy.$Proxy60.internalFrameOpened(Unknown Source)
at java.desktop/javax.swing.JInternalFrame.fireInternalFrameEvent(Unknown Source)
at java.desktop/javax.swing.JInternalFrame.show(Unknown Source)
at java.desktop/java.awt.Component.show(Unknown Source)
at java.desktop/java.awt.Component.setVisible(Unknown Source)
at java.desktop/javax.swing.JComponent.setVisible(Unknown Source)
at com.inductiveautomation.factorypmi.designer.workspace.WindowWorkspace$DesigntimeWindowOpener.openWindow(WindowWorkspace.java:3514)
at com.inductiveautomation.factorypmi.application.VisionDesktop.openWindow(VisionDesktop.java:197)
at com.inductiveautomation.factorypmi.application.script.builtin.NavUtilities.lambda$openWindowImpl$5(NavUtilities.java:393)
at com.inductiveautomation.factorypmi.application.script.builtin.WindowUtilities.doGUIAction(WindowUtilities.java:808)
at com.inductiveautomation.factorypmi.application.script.builtin.NavUtilities.openWindowImpl(NavUtilities.java:336)
at com.inductiveautomation.factorypmi.application.script.builtin.NavUtilities.openWindow(NavUtilities.java:326)
at com.inductiveautomation.factorypmi.application.script.builtin.NavUtilitiesDispatcher.openWindow(NavUtilitiesDispatcher.java:143)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:188)
at com.inductiveautomation.ignition.common.script.ScriptManager$ReflectedInstanceFunction.__call__(ScriptManager.java:519)
at org.python.core.PyObject.__call__(PyObject.java:480)
at org.python.core.PyObject.__call__(PyObject.java:484)
at org.python.pycode._pyx5.f$0(<event:actionPerformed>:7)
at org.python.pycode._pyx5.call_function(<event:actionPerformed>)
at org.python.core.PyTableCode.call(PyTableCode.java:171)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.Py.runCode(Py.java:1614)
at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:770)
at com.inductiveautomation.factorypmi.application.binding.action.ActionAdapter.runActions(ActionAdapter.java:200)
at com.inductiveautomation.factorypmi.application.binding.action.ActionAdapter.invoke(ActionAdapter.java:291)
at com.inductiveautomation.factorypmi.application.binding.action.RelayInvocationHandler.invoke(RelayInvocationHandler.java:57)
at com.sun.proxy.$Proxy58.actionPerformed(Unknown Source)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.desktop/java.awt.Component.processMouseEvent(Unknown Source)
at java.desktop/javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.desktop/java.awt.Component.processEvent(Unknown Source)
at java.desktop/java.awt.Container.processEvent(Unknown Source)
at java.desktop/java.awt.Component.dispatchEventImpl(Unknown Source)
at java.desktop/java.awt.Container.dispatchEventImpl(Unknown Source)
at java.desktop/java.awt.Component.dispatchEvent(Unknown Source)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.desktop/java.awt.Container.dispatchEventImpl(Unknown Source)
at java.desktop/java.awt.Window.dispatchEventImpl(Unknown Source)
at java.desktop/java.awt.Component.dispatchEvent(Unknown Source)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.desktop/java.awt.EventQueue$4.run(Unknown Source)
at java.desktop/java.awt.EventQueue$4.run(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.desktop/java.awt.EventQueue$5.run(Unknown Source)
at java.desktop/java.awt.EventQueue$5.run(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.desktop/java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at org.python.core.PyJavaType.wrapJavaObject(PyJavaType.java:90)
at com.inductiveautomation.factorypmi.application.script.PyComponentWrapper.<init>(PyComponentWrapper.java:57)
at com.inductiveautomation.factorypmi.application.script.PyComponentWrapper.__findattr_ex__(PyComponentWrapper.java:94)
at org.python.core.PyObject.__getattr__(PyObject.java:1025)
at org.python.pycode._pyx29.f$0(<event:internalFrameOpened>:24)
at org.python.pycode._pyx29.call_function(<event:internalFrameOpened>)
at org.python.core.PyTableCode.call(PyTableCode.java:171)
... 72 more
Ignition v8.0.8 (b2020021111)
Java: Azul Systems, Inc. 11.0.5
Error in vision:
Traceback (most recent call last):
File "<event:internalFrameOpened>", line 10, in <module>
TypeError: getComponent(): 1st arg can't be coerced to int
Ignition v8.0.8 (b2020021111)
Java: Azul Systems, Inc. 11.0.5