Solving Runtime errors

Is there any existing documentation on handling runtime errors? I find the information provided in AI2 quite rudimentary. If one has more than a thousand lines of code, a simple message such as "Attempting to read element[1] of a list with no elements" does not really help. At the least it would be helpful to know which block generated the runtime error. Otherwise, with a large source code, it becomes almost impossible to track down the error. Any suggestions?

Post a screenshot of the error message and show your (relevant) blocks.

See also here:

This is the exact problem. I have many, many blocks and I do not know which one is relevant. I suspect I have close to 100 blocks. If I knew which block to look at, I could solve the runtime error. This runtime error could be generated by any of about 6 reasonably sizeable blocks. I am proceeding by inserting a button called BRK that becomes visible and which loops until the button is pressed. This is effectively a breakpoint that can be inserted anywhere in the code. It is the only sane way that I can think of to proceed. Any suggestions will be extremely welcome. Thank you very much for your time.

... and post the aia.

Maybe this extension might help

See for example Catch runtime error / Identify the blocks that cause it - #18 by dora_paz

Use this event to get more details about the error message:


Add 3 Labels and put them into this event.
And post the result.


The code interacts with external Arduino-based hardware monitoring a Lithium battery (comprised of 8 cells) and which communicates using Bluetooth.
Attached are three images and the .aia file:

BatMon50.aia (605.8 KB)

An image of the UI in case this may help to understand any of the code
An image of the error message
An image of an example of the data transferred from the Bluetooth device to the ai2 app after pressing the Chart tab on the UI.

After lots of experimentation I believe the the runtime error is triggered by the 2nd last statement in the SelectTab method (setVerticalArrangement.visible). This is called from ChartSpinner1 which is, in turn, called from ChartsTab when the Charts tab is pressed on the UI. This was performed by inserting a return from a method before it completed to inspect when the runtime error is generated.

The general symptoms here remind me of something similar to a mismanaged pointer problem in C++. It's been decades since I coded in Java, so I cannot comment on the Java perspective.

This is my first project in AI2, so please feel free to comment on my bad programming style. For instance, the extensive use of global variables instead of local variables would normally be considered as bad practice. Maybe misuse of lists causes the error, although I have carefully and exhaustively checked the code.

Thank you very much for your time. It is very valuable to me.

What about my suggestion? Are you not getting any error messages in this event?

Unfortunately, we (at least I) don't have the time to open every aia in all current forums (AI2, Kodular, Niotron, ...) and dive deep into the blocks and logic of each project.

I have a similar block as part of my code to trap errors. Although as currently implemented, it only traps Bluetooth-related connection errors, I experimented extensively with this and the current runtime error is for some reason not trapped using this method. If you have any suggestions of why this is, I would really appreciate it. Thanks for your time.

I appreciate this totally. Since you asked that I post the .aia, would this be of any use to get closer to the problem?

Then try this event:


I now have two independent error trapping event catchers.
I still have the problem that the Screen1 error handler does not catch my runtime error. The message given is exactly the same as before. Attempt to access item 1 of list of length zero:()
If you have any idea I would highly appreciate it.
Thanks again for your time.
Screenshot from 2022-07-02 15-16-30

The problem is you are attempting to read a List that does not yet contain any information; possibly because of a timing issue.

Check all your code where you attempt to read values from a List and you will discover the problem. :cry: You possibly can use DoIt to debug.

Use the MIT debugging advice Live Development, Testing and Debugging

and Enis's comments about the built-in DoIt debgger

The error message indicates you have an issue. You do not want to trap the error. The message indicates it has been trapped. You want to FIX the error.

I have had a look at your blocks for the ChartSpinner. There do appear to be a couple of issues that might be causing the problem...

  1. You have 7 selections/items in the spinner (but you use item 1 as a header)
  2. in your after picking code:
  • you consider the selectionindex = 8, which is not possible (with only 7 items)?
  • you add 1 to the selection index which might take you over the 7 ?

Have a look at wherever you are selecting an item from a list in the afterpicking event and test it.

Spinners can display strange behaviour, this might help:

And what is the error with the parameter "funktionName"?

I looked at your .aia file.

Your BlueTooth message handling is based on immediate gratification, trying to pull in response data in the same events that request the data.

Most apps use only a Clock Timer for single message reads, and parse the incoming messages for data content.

They also don't make any assumptions as to what they will get when they split incoming messages. Tedious as it is, you have to check length of list before doing any select item from list.

By the Way, I notice you use CR as your message delimiter. Most people use NL (Decimal 10).

Evidence for the other Power Users:


Here is my standard boiler plate advice for BlueTooth Delimiters:

Please see the Delimiter article in FAQ

Be sure to use println() at the end of each message to send from the sending device, to signal end of message. Do not rely on timing for this, which is unreliable.

In the AI2 Designer, set the Delimiter attribute of the BlueTooth Client component to 10 to recognize the End of Line character.
Also, return data is not immediately available after sending a request,
you have to start a Clock Timer repeating and watch for its arrival in the Clock Timer event. The repeat rate of the Clock Timer should be faster than the transmission rate in the sending device, to not flood the AI2 buffers.

In your Clock Timer, you should check

  Is the BlueTooth Client still Connected?
  Is Bytes Available > 0?
     IF Bytes Available > 0 THEN
       set message var  to BT.ReceiveText(-1) 

This takes advantage of a special case in the ReceiveText block:

Receive text from the connected Bluetooth device. If numberOfBytes is less than 0, read until a delimiter byte value is received.

If you are sending multiple data values per message separated by | or comma, have your message split into a local or global variable for inspection before trying to select list items from it. Test if (length of list(split list result) >= expected list length) before doing any select list item operations, to avoid taking a long walk on a short pier. This bulletproofing is necessary in case your sending device sneaks in some commentary messages with the data values.

TIMAI2, Thank you very much for your comment. My code was actually correct but extremely confusing because I use the dropdown selection to indicate a data column to be graphed. Very bad practice in my specific case. I spent the last half day separating the dropdown selection from that data column to graph. In the process I uncovered a can of worms due to legacy issues but unfortunately the runtime error is not solved. Let me follow up some of the other suggestions below. Thanks very much for your time.

@ABG: Thank you very much. These are extremely valuable comments. I appreciate your time.


Screenshot from 2022-07-03 14-41-10

This is my error mechanism. It is not triggered at all by the runtime error. Since the above error reporting block is not triggered by the runtime error, the function name remains unknown.