I have a string -0.06,0.00,-0.99
. I want to parse the first number to a float.
I am using:
String myString = "-0.06,0.00,-0.99";
float myFloat = float(split(myString, ",")[0]);
println(split(myString, ",")[0]); // prints -0.06
The second line throws a NullPointerException
.
I have also tried the following and got the exact same error.
float myFloat = Float.parseFloat(split(myString, ",")[0]);
How do I parse a string to a float without getting a NullPointerException
.
Actual MVCE:
I am using processing to take input from my serial line by line. The first input is "-0.06,0.00,-0.99"
.
buffer = port.readStringUntil('\n'); //take input from serial til line-break
float[] acceleration;
if (buffer != null) {
if (buffer.indexOf(",") != -1) {
buffer = trim(buffer);
acceleration[0] = float(buffer.split(",")[0]); // this throws the error
}
}