mardi 8 avril 2014

ArrayIndexOutofBoundException - Java - Android - réception de flux de données et de stockage dans multidimensionnelle array Java - Stack Overflow


I am receiving 30000 strings in real time via bluetooth, about 1000 a second. I am parsing the data as a double and trying to store those 30000 data points into the multi-dimensional array[30000][1].However, when I run the Android application, it crashes after a few points due to an ArrayIndexOutofBoundException.


I do realize this can be done in a singular array, but I will eventually be increasing the width of the array.


Array Size of Stored = [30000][1]


Array Size of convert =[1]


BluetoothChat


case MESSAGE_READ:
for (int a = 0; a < 30000; a++) {
byte[] readBuf = (byte[]) msg.obj;
try {
String readMessage = new String(readBuf, 0, msg.arg1);
mConversationArrayAdapter.add("Voltage: " + readMessage);
double[] convert = new double[1];

for (int z = 0; z < 1; z++) {
convert[z] = Double.parseDouble(readMessage);
}
for (int j = 0; j < 1; j++) {
stored[a][j] = convert[a];
}
} catch (NumberFormatException e) {
System.err.println("NumberFormatException: " + e.getMessage());
}
}

finalValue = new HjorthClass(stored);
if (finalValue.returnSum() == true) {
seizureResult.setText("A Seizure has been detected");
}
break;

logcat;


04-04 22:44:17.406: E/AndroidRuntime(14619): FATAL EXCEPTION: main
04-04 22:44:17.406: E/AndroidRuntime(14619): java.lang.ArrayIndexOutOfBoundsException
04-04 22:44:17.406: E/AndroidRuntime(14619): at com.example.android.BluetoothChat.BluetoothChat$2.handleMessage(BluetoothChat.java:311)
04-04 22:44:17.406: E/AndroidRuntime(14619): at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 22:44:17.406: E/AndroidRuntime(14619): at android.os.Looper.loop(Looper.java:130)
04-04 22:44:17.406: E/AndroidRuntime(14619): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-04 22:44:17.406: E/AndroidRuntime(14619): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 22:44:17.406: E/AndroidRuntime(14619): at java.lang.reflect.Method.invoke(Method.java:507)
04-04 22:44:17.406: E/AndroidRuntime(14619): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-04 22:44:17.406: E/AndroidRuntime(14619): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-04 22:44:17.406: E/AndroidRuntime(14619): at dalvik.system.NativeStart.main(Native Method)



First you declare a double array of size 1.


double[] convert = new double[1];

Then you try to call that array convert[a] where a could be from 0 to 2999, as soon as a > 0 it is out of bounds.


stored[a][j]= convert[a]; // a > 0 ? if yes then out of bounds

There is your error. My guess is it should be:


stored[a][j]= convert[j];


I am receiving 30000 strings in real time via bluetooth, about 1000 a second. I am parsing the data as a double and trying to store those 30000 data points into the multi-dimensional array[30000][1].However, when I run the Android application, it crashes after a few points due to an ArrayIndexOutofBoundException.


I do realize this can be done in a singular array, but I will eventually be increasing the width of the array.


Array Size of Stored = [30000][1]


Array Size of convert =[1]


BluetoothChat


case MESSAGE_READ:
for (int a = 0; a < 30000; a++) {
byte[] readBuf = (byte[]) msg.obj;
try {
String readMessage = new String(readBuf, 0, msg.arg1);
mConversationArrayAdapter.add("Voltage: " + readMessage);
double[] convert = new double[1];

for (int z = 0; z < 1; z++) {
convert[z] = Double.parseDouble(readMessage);
}
for (int j = 0; j < 1; j++) {
stored[a][j] = convert[a];
}
} catch (NumberFormatException e) {
System.err.println("NumberFormatException: " + e.getMessage());
}
}

finalValue = new HjorthClass(stored);
if (finalValue.returnSum() == true) {
seizureResult.setText("A Seizure has been detected");
}
break;

logcat;


04-04 22:44:17.406: E/AndroidRuntime(14619): FATAL EXCEPTION: main
04-04 22:44:17.406: E/AndroidRuntime(14619): java.lang.ArrayIndexOutOfBoundsException
04-04 22:44:17.406: E/AndroidRuntime(14619): at com.example.android.BluetoothChat.BluetoothChat$2.handleMessage(BluetoothChat.java:311)
04-04 22:44:17.406: E/AndroidRuntime(14619): at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 22:44:17.406: E/AndroidRuntime(14619): at android.os.Looper.loop(Looper.java:130)
04-04 22:44:17.406: E/AndroidRuntime(14619): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-04 22:44:17.406: E/AndroidRuntime(14619): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 22:44:17.406: E/AndroidRuntime(14619): at java.lang.reflect.Method.invoke(Method.java:507)
04-04 22:44:17.406: E/AndroidRuntime(14619): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-04 22:44:17.406: E/AndroidRuntime(14619): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-04 22:44:17.406: E/AndroidRuntime(14619): at dalvik.system.NativeStart.main(Native Method)


First you declare a double array of size 1.


double[] convert = new double[1];

Then you try to call that array convert[a] where a could be from 0 to 2999, as soon as a > 0 it is out of bounds.


stored[a][j]= convert[a]; // a > 0 ? if yes then out of bounds

There is your error. My guess is it should be:


stored[a][j]= convert[j];

0 commentaires:

Enregistrer un commentaire