When testing a script, you may eventually come across an Error Message Box like the one shown below.
Your first inclination will be to close the error without closer examination: resist it!
The error messages generated from failed script executions are incredibly helpful. The Message tab on the error describes the Event Handler that encountered the exception; in this case, the actionPerformed event handler. This is important in cases where multiple Event Handlers on the same component have scripts. Without knowing which Event Handler generated the script, we could waste time trying to troubleshoot the wrong script.
Additionally, it shows the name of the component, which is "NavButton_MyWindow". Again, this is helpful if multiple scripts from multiple components are triggering in quick succession. The Message tab will clearly point you towards the source of the exception. This is another reason to give meaningful names to your components: doing so makes the process of tracking an error much easier.
The Details tab has even more information, specifically the line number that the exception occurred on, as well as the error message. The message here states that the window at path "MyWindow" does not exist, so we can check the Project Browser to see if we simply mistyped the name of the window.
Troubleshooting Errors Using the Error Message Box
When we don't get our expected results from our script, as in the following examples, always read both the Message and Details tabs in the Error Message Box. They are pretty good about pointing you to the root cause of your error. From there, you can easily find and fix any errors in your script.
The following examples show how to troubleshoot some of these error messages from the Error Message Box. Keep in mind that every script is unique, but at least you'll become familiar with what some of the error messages mean, and gain a little insight of the troubleshooting process.
Broken Example - Incorrect Attribute
Here is a script that when a Button is pressed, it's supposed to open a Message Box on a window and display "Hello World".
From the Error Message Box, you know two important things right away: the script is using the actionPerformed event on the Hello Button, and line 1 displays the description of the error. Check to be sure you used the correct attribute. Then check the spelling, case sensitive letters, spacing, and operators in your script. If you are familiar with Ignition's Built-in Scripting Functions, you can probably spot the error immediately. If not, you might want to use the autocompletion popup feature to retype your 'messageBox' scripting function. This will automatically fix any syntax errors.
The error in this example is in the attribute name. It uses case sensitive letters (i.e., messageBox).
In this case, "messagebox" should be spelled with a capital "B" since we're trying to use Ignition's system.gui.messageBox function. We corrected the script by changing our code to the following:
Broken Example - Undefined
Here is a similar script that when a Button is pressed, it's supposed to open a Message Box on a window and display "Testing".
The Error Message Box displays an error on the actionPerformed script on the button component in line 1 with an undefined name. The Details tab is simply telling us that something is not defined in the script. You can either define a variable or create a string by putting "Testing" in quotes to correct the error.
For this example, we corrected the code by turning the argument passed to system.gui.messageBox into a string literal with quotation marks:
Broken Example - Type Error
Similar to the other scripts, this script opens a Message Box and is supposed to display "100" when a Button is pressed.
Since this script failed to execute successfully, the Error Message Box popped up and displayed an error on the actionPerformed script on the button component in line 1 with a TypeError. This error tells us that the 1st parameter is expecting a string, not an integer.
For this example, we corrected the code by changing the argument passed to system.gui.messageBox into a string literal with quotation marks.
You can see from the examples above, that the Error Message Box provides quite a bit of information that points to the root cause of an error on an Event Handler script. As you learn from the Error Message Boxes about what these messages mean, you'll be able to quickly spot them in your script and quickly correct any errors you encounter.