1

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
  }
}
nonsequiter
  • 663
  • 3
  • 9
  • 22

3 Answers3

0

Java contains wrapper classes around the primitive types. For example, there is a Float class that wraps the float primitive. These wrapper classes have valueOf(String s) functions, which can parse a String representation of those primitive types into their actual primitive forms. Calling Float.valueOf(stringRepresentationOfAFloat) will give you a float variable if it is in the correct format.

See my code below for an example:

    String floatCsv = "-0.06,0.00,-0.99";
    String [] floatCsvSplit = floatCsv.split(",");
    for(String floatStringValue : floatCsvSplit){
        float floatVariable = Float.valueOf(floatStringValue);
        System.out.println(floatVariable);
    }
Mike Elofson
  • 2,017
  • 1
  • 10
  • 16
  • This is Processing, not just Java. See the reference for the `float()` function: https://www.processing.org/reference/floatconvert_.html – Kevin Workman Nov 12 '15 at 19:47
0

Try this:

String myString = "-0.06,0.00,-0.99";
float myFloat = Float.parseFloat(myString.split(",")[0]);
System.out.println(myFloat); // prints -0.06
Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
  • This is Processing, not Java. See the reference for the `split()` function: https://www.processing.org/reference/split_.html – Kevin Workman Nov 12 '15 at 19:46
  • I think it should be fine to use either Processing's wrapped functions or Java's original String methods, as long as point get's across. – George Profenza Nov 16 '15 at 15:23
  • @GeorgeProfenza I don't disagree. But OP's problem had nothing to do with the `parse()` or `Float.parseFloat()` functions, so this answer doesn't really help much. – Kevin Workman Nov 16 '15 at 15:31
  • true, the error is a null pointer exception, so uninitialised data as Kenney pointed out 4 days ago – George Profenza Nov 16 '15 at 15:37
-1

Are you perhaps running Java 1.7? There is a bug in that version, resolved in 1.8, where String.split() returns one empty element at the beginning of the array.

Try printing split(myString, ","); you should see something like ["", "-0.06", "0.00", "-0.99"].

See this SO question for more information.

Community
  • 1
  • 1
Kenney
  • 9,003
  • 15
  • 21
  • Ok, was worth a shot ;-) So, if you print `split(myString, ",")` you do see `["-0.06", "0.00", "-0.99"]`? – Kenney Nov 12 '15 at 20:00
  • Hmm... I ran this `println(split(myString, ",")); ` which output `-0.06 0.00 -0.97`. Oddly using `System.out.println(split(myString, ","))` gave me `[Ljava.lang.String;@1c9570e4`. – nonsequiter Nov 12 '15 at 20:05
  • You'll want to use [`Arrays.toString`](http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#toString%28java.lang.Object[]%29) for that second println. – Kenney Nov 12 '15 at 20:11
  • This is Processing, not Java. The `println()` function handles arrays just fine. – Kevin Workman Nov 12 '15 at 23:58
  • @KevinWorkman `System.out.println` is Java, AFAIK - as well as `[Ljava.lang.String;@1c9570e4` – Kenney Nov 13 '15 at 00:06
  • @Kenney Yes, it is. But Processing also has a `println()` function: https://processing.org/reference/println_.html – Kevin Workman Nov 13 '15 at 00:07
  • @KevinWorkman **Note** That using Processing's ```println()``` will print the array only if the array is alone in the statement(e.g. isn't concatenated with a string), otherwise the the object's address in memory is listed: ```String[] s = {"0.1","-0.5","0.3"}; println(s); println("values: " + s); println("values:"); printArray(s);``` – George Profenza Nov 16 '15 at 15:29