# HELP! Exception in Event Handler

**URL:** https://discourse.processing.org/t/help-exception-in-event-handler/37751
**Category:** Electronics (Arduino, etc.)
**Created:** [June 28, 2022, 7:31pm UTC](https://discourse.processing.org/t/help-exception-in-event-handler/37751 "2022-06-28T19:31:25Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![charlielamus](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@charlielamus](https://discourse.processing.org/u/charlielamus)
#### Post date: [June 28, 2022, 7:31pm UTC](https://discourse.processing.org/t/help-exception-in-event-handler/37751/1 "2022-06-28T19:31:25Z")

</div>

Hello!

I know next to nothing about java, being limited to playing about with the Marlin code for my 3d printer.

I am a total beginner, so please be kind… I am also autistic so I might ask questions which sound stupid. Sorry.

I have downloaded some code called “knitty” from here:

[https://github.com/knitty/gui](https://github.com/knitty/gui)

and it runs beautifully on Processing 2.2.1, except that when I press the button marked “connect to arduino”, it produces the following error message, which has me totally baffled:

################ EXCEPTION IN EVENT HANDLER ################  
An error occured during execution of the eventhandler:  
CLASS: knitty\_main METHOD: handleButtonEvents  
Caused by java.lang.ArrayIndexOutOfBoundsException: 1  
knitty\_main.initSerialCommunication(knitty\_main.java:1987)  
knitty\_main.handleButtonEvents(knitty\_main.java:1110)  
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)  
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)  
java.lang.reflect.Method.invoke(Unknown Source)  
g4p\_controls.GAbstractControl.fireEvent(Unknown Source)  
g4p\_controls.GButton.mouseEvent(Unknown Source)  
g4p\_controls.GWindowInfo.mouseEvent(Unknown Source)  
sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)  
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)  
java.lang.reflect.Method.invoke(Unknown Source)  
processing.core.PApplet$RegisteredMethods.handle(PApplet.java:1236)  
processing.core.PApplet.handleMethods(PApplet.java:1431)  
processing.core.PApplet.handleMouseEvent(PApplet.java:2826)  
processing.core.PApplet.dequeueEvents(PApplet.java:2725)  
processing.core.PApplet.handleDraw(PApplet.java:2397)  
processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:240)  
processing.core.PApplet.run(PApplet.java:2256)  
java.lang.Thread.run(Unknown Source)  
##############################################################

The “handleButtonEvents” seems to work for all the other buttons, so I have been looking at the initSerialConnection function, which is reproduced here:

```auto
void initSerialCommunication() {
  serialCommunication = new Serial(this, Serial.list()[serialport], 115200);
  serialCommunicationStarted = 1;
}

```

and it is making me sweat… I don’t know what I am doing; am I doing this wrong? The code has worked for other people so what might be happening?

Thank you in advance!

Charlie

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [June 29, 2022, 2:12am UTC](https://discourse.processing.org/t/help-exception-in-event-handler/37751/2 "2022-06-29T02:12:14Z")

</div>

Hello @charlielamus ,

I do not use Processing 2.2.1 but did get this to run on Processing 3.5.4 and Processing 4.0b8.

Changes:

```auto
// Add this:
void settings()
{
size(GUIwidth, GUIheigth);
}

void setup() {
 // size(GUIwidth, GUIheigth); // Comment this

```

```auto
import drop.*; // Added this library
import test.*;

// Don't forget to place the libraries under sketchbook/libraries/G4P and sketchbook/libraries/sDrop
import g4p_controls.*; // http://sourceforge.net/projects/g4p/
//import sojamo.drop.*; // http://www.sojamo.de/libraries/drop/ // Commented this!

```

There is firmware that comes with Knitty:

> **[Knitty Project](https://github.com/knitty)**
>
> Reverse-Engineering of an Superba/Singer 624/Memo II and Passap/Pfaff E6000 - Knitty Project

Steps:

- Uploaded one of the Knitting Machine (Arduino firmware) selections to Arduino.
- Run the Knitty Processing sketch.
- Select the Arduino COM Port that is being used.
- Select the Knitting Machine that is being used.

I selected “Knit One Row” from GUI:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/1/2/12740b25faa3e44acf3ad892dc7355e559971b1a.png)

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/9/f/9f2b3f19dcf06d69d895eb18da8bfda656a0d9b0.png)

`:)`

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [June 29, 2022, 8:31am UTC](https://discourse.processing.org/t/help-exception-in-event-handler/37751/3 "2022-06-29T08:31:12Z")

</div>

This is called a stack trace and is generated when an exception is thrown.

1. ################ EXCEPTION IN EVENT HANDLER ################
2. An error occured during execution of the eventhandler:
3. CLASS: knitty\_main METHOD: handleButtonEvents
4. Caused by java.lang.ArrayIndexOutOfBoundsException: 1
5. knitty\_main.initSerialCommunication(knitty\_main.java:1987)
6. knitty\_main.handleButtonEvents(knitty\_main.java:1110)
7. sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
8. sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
9. sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
10. java.lang.reflect.Method.invoke(Unknown Source)
11. g4p\_controls.GAbstractControl.fireEvent(Unknown Source)
12. g4p\_controls.GButton.mouseEvent(Unknown Source)

It basically lists the methods executed just before the exception was encountered. I have only shown twelve lines because that is the interesting part.

Line 12 is the method called to test whether an event occurred on a G4P button. In line 11 we see that the fireEvent method is called which will invoke (execute) the event handler. Lines 7, 8 and 9 are Java functions which locate the event handler (`handleButtonEvents`) and execute it.

Line 6 is calling the event handler where you check which button is clicked and executes your code.

Line 5 is calling your method `initSerialCommunication` and line 4 shows the cause of the exception, in this case an attempt to access an array element that doesn’t exist. So now we are back at your code

```auto
void initSerialCommunication() {
  serialCommunication = new Serial(this, Serial.list()[serialport], 115200);
  serialCommunicationStarted = 1;
}

```

Obviously `Serial.list()` returns an array and `serialport` indicates the element in the array you are trying to access. So change the method to

```auto
void initSerialCommunication() {
  println("Array length " + Serial.list().length);
  println("Element " + serialport);
  // serialCommunication = new Serial(this, Serial.list()[serialport], 115200);
  // serialCommunicationStarted = 1;
}

```

This exception will be thrown if

- if the array length is 0 (zero)
- if the element number is \>= array length

One last thing all G4P controls have a `tag` attribute which can be used to help debug your code. For instance immediately after creating a button called `my_button` add a statement to give a tag value e,g,

`my_button.tag = "Serial button clicked"; // or whatever you want`

If you give all your buttons different tag values then you can print the tag value when a problem occurs.
