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(String.java:4606)
	at java.base/java.lang.String.substring(String.java:2709)
	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(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at processing.core.PApplet$RegisteredMethods.handle(PApplet.java:1309)
	at processing.core.PApplet.handleMethods(PApplet.java:1456)
	at processing.core.PApplet.handleDraw(PApplet.java:2106)
	at processing.awt.PSurfaceAWT$9.callDraw(PSurfaceAWT.java:1386)
	at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:356)
         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() {
  mainPage.show();
  background(#C5DFF8);
  fontHead = createFont("AdobeHeitiStd-Regular", 25);
  textFont(fontHead);
  textAlign(CENTER);
  stroke(#A0BFE0);
  fill(#A0BFE0);
  stroke(#A0BFE0);
  fill(#A0BFE0);
  rect(55*10/10, 160*10/10, 390*10/10, 220*10/10);

  stroke(#333333);
  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 mainPage.show(); 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.

4 Likes

Thanks for the catch!

I will try this out and get back.

Hi,

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.