index out of bounds when multiplying a double
-
13-12-2019 - |
Question
First time posting. Thanks for the help in advance.
I have two activities. First activity reads in 3 numbers from the user via edittext fields whose inputType is "numberDecimal". It bundles these and sends them to the second activity via intent.
The second activity reads in the 3 numbers. It then needs to multiply them by some constants. That's where the problem comes in. If the result of the multiplication is too large (say, greater than a couple hundred...not constant threshold...not close to what a double should be able to hold) then it throws an indexoutofboundsexception.
//@SuppressWarnings ("serial)")
public class CreationActivity extends Activity {
@Override
public void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.creation);
TextView txtTitle = (TextView) findViewById(R.id.labelTitle);
TextView txtArtist = (TextView) findViewById(R.id.labelArtist);
TextView txtTime = (TextView) findViewById(R.id.labelTitle);
try {
Bundle extras = getIntent().getExtras();
double Hours;
double Minutes;
double Seconds;
if (extras!=null){
double timerTime[] = extras.getDoubleArray("Time");
Hours = timerTime[0];
Minutes = timerTime[1];
Seconds = timerTime[2];
//next line is where the error happens:
double desired_time = timerTime[0] * 5000;
//do some stuff
}else{
txtTitle.setText("No data");
}
} catch (NumberFormatException e){
}
}
}
And the LogCat is:
07-27 21:58:41.736: E/AndroidRuntime(31560): FATAL EXCEPTION: main
07-27 21:58:41.736: E/AndroidRuntime(31560): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.decker.timer/com.decker.timer.CreationActivity}: java.lang.IndexOutOfBoundsException: Invalid index 4, size is 4
07-27 21:58:41.736: E/AndroidRuntime(31560): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-27 21:58:41.736: E/AndroidRuntime(31560): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-27 21:58:41.736: E/AndroidRuntime(31560): at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-27 21:58:41.736: E/AndroidRuntime(31560): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-27 21:58:41.736: E/AndroidRuntime(31560): at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 21:58:41.736: E/AndroidRuntime(31560): at android.os.Looper.loop(Looper.java:137)
07-27 21:58:41.736: E/AndroidRuntime(31560): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-27 21:58:41.736: E/AndroidRuntime(31560): at java.lang.reflect.Method.invokeNative(Native Method)
07-27 21:58:41.736: E/AndroidRuntime(31560): at java.lang.reflect.Method.invoke(Method.java:511)
07-27 21:58:41.736: E/AndroidRuntime(31560): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-27 21:58:41.736: E/AndroidRuntime(31560): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-27 21:58:41.736: E/AndroidRuntime(31560): at dalvik.system.NativeStart.main(Native Method)
07-27 21:58:41.736: E/AndroidRuntime(31560): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 4, size is 4
07-27 21:58:41.736: E/AndroidRuntime(31560): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
07-27 21:58:41.736: E/AndroidRuntime(31560): at java.util.ArrayList.get(ArrayList.java:304)
07-27 21:58:41.736: E/AndroidRuntime(31560): at com.decker.timer.CreationActivity.onCreate(CreationActivity.java:172)
07-27 21:58:41.736: E/AndroidRuntime(31560): at android.app.Activity.performCreate(Activity.java:4465)
07-27 21:58:41.736: E/AndroidRuntime(31560): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-27 21:58:41.736: E/AndroidRuntime(31560): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-27 21:58:41.736: E/AndroidRuntime(31560): ... 11 more
07-27 21:58:49.260: D/TimerActivity(31583): onClicked
Solution
It sounds like you are trying to access the 5th (item at position 4) item in an array of length four.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow