Trying to write a script in a button on the main window.
Whenever click on this button, it will check another component(Eg a Cylindrical Tank) in another window(tanks).
If the tank is filled with water(Eg water volume is some value), then it will try to set its value to 0.
Am able to get the value of the tank.
However, i couldnt find anything or command to set the component to 0.
As there isnt such command: window.rootContainer.setComponent(“tank1”).value = 0
Also i search through the forum with the above topic/title keywords but it didnt find anything related. Thus writing here for any advise or assistance.
Below is my script, it gives error of
java.lang.NullPointerException: java.lang.NullPointerException
caused by NullPointerException
First, here’s what you’re asking for-- the main gotcha is straight from the scripting docs – if the window is not open, you’ll get an error because you can only access open windows. The other gotcha – is maybe your tank isn’t in the rootContainer because you’ve moved it into some sort of panel/container and now your button breaks without you realizing.
# Start the try block in case the window is not open.
try:
# Grab the window reference and assign it to the variable window.
window = system.gui.getWindow("tanks")
window.getRootContainer().getComponent("tank1").value = 0
# Handle the exception by opening an informative error.
except ValueError:
system.gui.errorBox("The window is not open!", "Error")
Next, you might be going about this from the wrong angle. So here’s some assumptions:
Your tank is(or should be) bound to a Tag representing tank level that is pulling from some hardware or simulated in software.
You want to be able to reset tank level whether or not the window is active(via script, timer, or whatever)
In that case you’d just set the value of the tag that the tank value is bound to like:
Thank you very much for your insightful and comprehensive advises and great assistance.
Yes, you are absolutely right on the dot.
When I run the script trying to access the window that is not opened, I encountered error .
So far, didnt encounter the other error as i am not yet familiar, so i didnt move the component(tank1) around but it is still residing in the root container i suppose . But definitely noted the point that you highlighted in advance.
Next, it is most grateful that you are so sharp to point out those assumptions and that i am coming from the wrong angle.
Right now, i am just trying out, so i just manually key in some values for those tanks initially and try to see how to reset it.
Also, I am still figuring out whether to get the tanks’ values from the hardware directly or from the database(mysql).
As of now, I dont have any hardware connected to Ignition yet, so those tanks components in Ignition are not bounded to any tag yet.
However, in time to come, the actual implementation is that all the tanks(around 20 of them) would be connected to the sensors and that Ignition need to be able to read the values of those sensors and then able to reset those values. Yes, whether or not the window is active, I would need to be able to reset the tanks’ level.