We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi All-
I'm trying to troubleshoot a bug that's been eluding me for a while now. Below is the first few lines of the stacktrace that is returned when it pops up. Does anyone know what "1967" refers to in the second line.
java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.substring(String.java:1967) at controlP5.ControlFont.calculateHeight(Unknown Source) at controlP5.ControlFont.adjust(Unknown Source) at controlP5.Label$MultilineLabel.draw(Unknown Source)
Answers
Look at your code
Which line is causing this? Almost certainly the substring command. The string used or one of the parameters is -1 and not allowed to be used with substring
Show your code
Thanks Chrisir. I've already figured that out. Unfortunately it isn't realistic to post my code. I have thousands of lines and multiple threads. I'm also using several libraries and I'm interacting with an Arduino over serial and hundreds of text files. Nobody would be able to duplicate the error with just the code. I've been injecting println commands into the code to try to figure out where the problem is happening but as of yet I haven't been able to pinpoint it. What I was trying to figure out with this question is how to decipher some of the stack trace codes. Specifically, what does 1967 refer to?
Just post the line that caused the error
Maybe it refers to the line number of Java's String::substring() method's source code. :-/
But the real error is the -1 being used as an index, which is obviously rubbish. Are you calling indexOf without checking the result?
In other words, the problem is in YOUR code.
(which is good, because that means we can fix it)
@koogs, according to the stack trace, it is the controlP5.ControlFont which is invoking String::substring(). :-B
@typ0 has also mentioned he's doing threads: L-)
So he is feeding Label$MultilineLabel the wrong parameters maybe use min(20, ......) ?
Hi All-
Thanks for the responses. Ive made some progress on this.
I developed a gui using controlP5. One of the components in the gui is a text area which I'm using as a console. I have it set up as follows:
I then created a function to post messages to the console and allow it to scroll:
The console is then updated every loop through draw(). I researched ways to use a system.out with controlP5 but I don't think its possible. This was what I came up with instead. It works except for the occasional random exception noted in my first post. Sometimes it will run for hours with many thousands of messages having scrolled through the console before receiving the exception. Sometimes it happens fairly quickly after starting. Yesterday I discovered if I comment out everything except println(txt); in the displayln function the issue never comes up. However, I've added println messages before and after the s=s.substring(1000) line and this doesn't appear to be the problem. What I'm thinking now is that, as @GoToLoop mentioned, controlP5 is using the substring method during the draw() function. Any thoughts?
Forgot to mention....the messages being sent to the console are coming from the second thread. I'm using two threads to avoid a stale gui during nested loops that take hours to finish.
www.sojamo.de/libraries/controlP5/examples/extra/ControlP5console/ControlP5console.pde
I get the same thing running this example when I enter 'case 3'
The controlP5 console example worked perfectly for me. It is extremely unlikely that the problem is due to a bug in controlP5 because it is a very popular libray used by thousands and someone is likely to have experienced it before.
In the method displayln(...) is the variable
s
being shared by both threads?If it is then that could be your problem -
s
is being changed between testing its length and callingsubstring(...)
Note
substr(int beginIndex)
return aStringIndexOutOfBoundsException
ifa) beginIndex < 0 or
b) beginIndex > the string length
The numeric value reported in the eception is
length - beginIndex
@quark looks like this is a bug in controlP5. I found the following on the GitHub issues page for the project:
https://github.com/sojamo/controlp5/issues/9
I also modified the example as follows and was able to determine that the exception is thrown on draw(). What's strange is that it doesn't always happen right away after setMax is set. On one try I made it through draw() 21 times before the exception was thrown. Sometimes it is thrown the first time through.
The exception and behavior are consistent with what I am seeing in my project...although I'm not using the setMax() method.
My mistake it looks like a bug in controlP5. I should have looked at the stacktrace more closely.
I have looked at the controlP5 source code and can confirm there is a possible bug in the
ControlFont
class, thecalculateHeight(...)
method.Theoretically this method can throw a StringIndexOutOfBoundsException under certain circumstances but looking at the source code it is difficult to see how these circumstances might arise.
Are you using controlP5's
Println
class in your program?I am using a function that I wrote to update the text area:
If I comment out everything except the println(txt) line the exception never happens. I have also verified that it isn't the call to substring in my function that generates the exception. Here is the exception I get:
I also tried using the technique in the console example instead of my displayln function and I get the same exception.
I take that to mean you are not using the
Println
class from controlP5.Are you calling
displayln(String txt)
from a secondary thread?Correct..I'm not using
Println
in my project. I am callingdisplayln(String txt)
from my second thread. I call a second thread to allow my gui to continue to update (including the text area that I'm using as a console).OK I suspect that the problem is that the
calculateHeight
method which is being called in the main GUI thread is being interrupted by the second thread excuting thedisplayln
method.In your
displayln
method comment out the last line only and try that i.e.thanks @quark..I tried that and I don't get the exception. I'm not set on using my
displayln
function. I tried to use the same technique as in the console example which uses thePrintln
class. This generated the same exception. I guess its similar though since with this technique I am callingprintln
from the second thread. Is there a better way to create a console which can receive messages from a second thread?On second thought, I'm questioning if using a second thread is the problem. the console example is a single thread and the same exception is generated. When I comment out the
Output.setText(s)
line in my code the textarea just stays blank. Perhaps the issue is more related to what is being displayed in the textarea/console?The console example is not single threaded!
The
Println
class creates a runnable thread that redirects theSystem.out.println()
to a buffer which is then read and appended to theTextarea
control.As I said before I examined the source code and although theoretically possible iI can see the circumstances that would cause the code to generate the exception.