I've encountered this issue with ControlP5 when using the TextArea object:
When calling 'setText(fooString)
' if fooString contains a ' ' in a line by itself, Java will throw a null pointer exception.
For example:
void setup()
{
size(300,300);
cp5 = new ControlP5(this);
textArea = cp5.addTextarea("txt")
.setPosition(0,0)
.setSize(30,30)
.setFont(createFont("arial",12))
.setLineHeight(14)
.setColor(color(0))
.setColorBackground(color(0))
.setColorForeground(color(255,100));
;
String s = "Hello\n \nworld";
textArea.setText(s);
}
Java throws: "StringIndexOutOfBoundsException
" and "NullPointerException
"
My workaround (ignoring the case that s = "[a bunch of spaces]"):
s = s.replaceAll("\r", "\n"); // Use just one type of new line
s = s.replaceAll("\n +", "\n"); // Case 1
s = s.replaceAll(" +\n", "\n"); // Case 2
Is there a better way to fixing this or is a bug (on my part) with the above code?
import controlP5.*; ControlP5 cp5; Textarea textArea; void setup() { size(300,300); cp5 = new ControlP5(this); textArea = cp5.addTextarea("txt","txt",0,0,30,60); String s = "Hello\n \nworld"; textArea.setText(s); }
with 0.5.4 and it works fine