String Index Out Of Bounds Exception

I am encountering a java.lang.StringIndexOutOfBoundsException when receiving data from a TCP server in my Processing application. The error occurs after sending a configuration command and receiving an acknowledgment from the server. Here’s the relevant part of the error message:

My GUI is interacting with an Arduino which is acting as a TCP server.
When I send some command it returns an acknowledgement. My command is being received there properly but I think when I get the ack, I am getting this error

java.lang.StringIndexOutOfBoundsException: begin 0, end -1, length 0
	at java.base/java.lang.String.checkBoundsBeginEnd(
	at java.base/java.lang.String.substring(
	at controlP5.ControlFont.calculateHeight(Unknown Source)
	at controlP5.ControlFont.adjust(Unknown Source)
	at controlP5.Label$MultilineLabel.draw(Unknown Source)
	at controlP5.Label.draw(Unknown Source)
	at controlP5.ControllerGroup.draw(Unknown Source)
	at controlP5.ControllerGroup.drawControllers(Unknown Source)
	at controlP5.ControllerGroup.draw(Unknown Source)
	at controlP5.ControlWindow.draw(Unknown Source)
	at controlP5.ControlWindow.draw(Unknown Source)
	at jdk.internal.reflect.GeneratedMethodAccessor3.invoke(Unknown Source)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(
	at java.base/java.lang.reflect.Method.invoke(
	at processing.core.PApplet$RegisteredMethods.handle(
	at processing.core.PApplet.handleMethods(
	at processing.core.PApplet.handleDraw(
	at processing.awt.PSurfaceAWT$9.callDraw(
	at processing.core.PSurfaceNone$
         StringIndexOutOfBoundsException: begin 0, end -1, length 0

It seems to be related to the ControlFont class, which is part of the ControlP5 library used for GUI elements. However, I’m not directly interacting with this class in my code. The error occurs in the background while the GUI is being updated.

Has anyone encountered a similar issue with ControlP5 or have any insights into what might be causing this error?

Here is my code :

I am no expert in controlP5 but if we look at the draw method in file STL_support_without_ports.pde we find

void draw() {;
  fontHead = createFont("AdobeHeitiStd-Regular", 25);
  rect(55*10/10, 160*10/10, 390*10/10, 220*10/10);

  line(150*10/10, 183*10/10, 307*10/10, 183*10/10);
  image(BEL_logo, 6*10/10, 4*10/10, 150*10/10, 65*10/10);

I assume that the statement; causes the controlP5 controls to be drawn.

My concern is the third statement -
fontHead = createFont("AdobeHeitiStd-Regular", 25);

Now the draw() method is executed ~60 times per second so creating the font 60 times per second is likely to cause problems. It would be usual to create the font in setup and use it in draw.

This might be the cause of the exception.


Thanks for the catch!

I will try this out and get back.


I tried this and the problem still exists.
But I have taken your advice and moved the line to setup.

I have replaced the controlp5 textArea with text() in processing and it is working for me except for the scrolling functionality that controlp5 gives.
It will work for me for now.