Bug 498 - Undo command crashes the program when there are no actions to undo
Summary: Undo command crashes the program when there are no actions to undo
Status: IN_PROGRESS
Alias: None
Product: Group 4
Classification: Unclassified
Component: Main (show other bugs)
Version: unspecified
Hardware: PC Mac OS
: Normal major
Deadline: 2020-11-14
Assignee: aaronlii2-c
URL:
Depends on:
Blocks:
 
Reported: 2020-11-28 19:24 HKT by kykao2-c
Modified: 2020-11-29 11:41 HKT (History)
4 users (show)

See Also:


Attachments
Program crashed when using the undo command (84.27 KB, image/png)
2020-11-28 19:24 HKT, kykao2-c
Details

Note You need to log in before you can comment on or make changes to this bug.
Description kykao2-c 2020-11-28 19:24:35 HKT
Created attachment 62 [details]
Program crashed when using the undo command

The program crashed upon the "undo" command when there is nothing to undo.

Severity: Major. 

Priority: Normal.

Specification of the device which encountered this problem:
Macbook Pro 2017, 13-inch
MacOS: Mojave version 10.14.6
Processor: 2.3 GHz Intel Core i5
Memory: 16 GB 2133 MHz LPDDR3
Graphics: Intel Iris Plus Graphics 640 1536 MB

Steps to reproduce the bug:
input:  1. "undo"
output:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 0
	at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
	at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
	at java.base/java.util.Objects.checkIndex(Objects.java:372)
	at java.base/java.util.ArrayList.remove(ArrayList.java:535)
	at managementSystem.RecordedCommand.undo(RecordedCommand.java:17)
	at managementSystem.CommandHandler.handle(CommandHandler.java:108)
	at managementSystem.Main.main(Main.java:20)

Process finished with exit code 1

Observation:
"undo" on empty arraylist accesses an invalid index and produces exception, which breaks the program.

Expectation:
The program returns an error message and asks the user to enter a new input without breaking the program whenever an undo command is used under the circumstance that there is nothing to undo.

Expected output:
Setting: there is nothing to undo.
input:  "undo"
output: "Nothing to undo."
The program does not end and the user should be able to enter any other input afterwards.

Solution:
Ensure the program does not access the invalid array index bound, eg. -1.
Comment 1 aaronlii2-c 2020-11-29 11:41:38 HKT
Bug fixed. Code for preventing the array out-of-bound exception problem is added as follows:
if (!undoList.isEmpty()) {
   //undo a command
} else {
   System.out.println("Nothing to undo.");
}

- Aaron