HELP! Exception in Event Handler

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

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:

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

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:

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

void setup() {
 // size(GUIwidth, GUIheigth); // Comment this
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:

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:

:)

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

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

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.

3 Likes