-1

Hi i have a string of numbers and want to put them into a float array so i can access the values.

// this bit works fine, data is split into string array.
String fdata[] = data.split(",");
Float array_f[] = new Float(fdata.length);

// this is the bit which throws an error and causes my app to crash.
for (int i = 0; i < fdata.length; i++){
    float y = Float.parseFloat(fdata[i]);
    array_f[i] = y;
}

The error is a

java nullpointerexception: attempt to invoke virtual method java.lang.string java.lang.split(java.lang.string) on a null object reference.

thanks

adding to this i think i need to explain a bit more, so i provided more code to show what im trying to do.

String data; //does this need to define the number of bytes needed then?

start_Acq.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    channel_a = String.valueOf(channel_Acq.getSelectedItem());
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                connection.start_acquisition(channel_a);
                data = connection.start_acquisition(channel_a);
                connection.stop_acquisition();
            }catch (Exception e ){
                e.printStackTrace();
            }
        }
    });thread.start();

    data_recived.setText(data);
    String fdata[] = data.split(",");
    Float array_f[] = new Float[fdata.length];

    for(int i = 0; i<fdata.length; i++){
        float y = Float.parseFloat(fdata[i]);
        array_f[i] = y;
    }

}
});
RedP
  • 25
  • 3

4 Answers4

3

the problem with your code is the initialization, all you have to do is to use the primitive float instead of Float :

 String data = "123,5468,1,25,36";
    String fdata[] = data.split(",");
    float array_f[] = new float[fdata.length];
    for (int i = 0; i < fdata.length; i++){
        array_f[i] = Float.parseFloat(fdata[i]);
System.out.println(array_f[i]);
}
Elias
  • 89
  • 10
  • @llyasse i am just confusing myself i think, i am still learning. I have updated my question, but say with my global variable data, when initialising do i have to define it with an allocated amount of space for when it receives the data. as it needs store 32 x 16384 bytes. – RedP Mar 08 '17 at 14:22
  • no you don't have to specify the amount of space but you need to make sure that data is stored inside your "data" variable, as the errors shows you're trying to do a split for a null object ! – Elias Mar 08 '17 at 17:01
  • @llyasse i think the problem is that the program is moving to fast to complete the previous, for instance it is trying to split the data variable while it is still acquiring the data variable from the DAQ. When breakpoints are added and taken one step at a time it works fine. Any advise – RedP Mar 09 '17 at 09:57
  • yes i think you should add thread.join() after thread.start(). The join call waits for the thread to terminate before returning. – Elias Mar 09 '17 at 11:53
  • Yeah it works now doesnt pass a null string. Thanks for your help – RedP Mar 17 '17 at 16:28
1

First of all the data is null; fix it and try by this

float y = java.lang.Float.parseFloat(fdata[i]);
1

Your array initialization is wrong, it should be like

Float array_f[] = new Float[fdata.length];

after length you should have a semicolon not ','

for (int i=0; i<fdata.length;i++){

EDIT Try this it will work

String fdata[] = new String[]{"6.5", "8.5"};           //this bit works fine, data is split into string array.
        Float array_f[] = new Float[fdata.length];
        for (int i = 0; i < fdata.length; i++) {          //this is the bit which throws an error and causes my app to crash.
            float y = Float.parseFloat(fdata[i]);
            array_f[i] = y;
        }
        System.out.println(Arrays.asList(array_f));
Arpan Sharma
  • 2,142
  • 11
  • 22
0

use this get string array to string then convert float

 for(int i=0;stringarray.length>i;i++){float_var = Float.valueOf(string_array.get(i)); float_array.add(float_var);}

then add float array

Akash pasupathi
  • 304
  • 1
  • 14