Bug 773 - ArrayIndexOutOfBoundsException
Summary: ArrayIndexOutOfBoundsException
Status: RESOLVED FIXED
Alias: None
Product: Gear and Member Management System for library
Classification: Unclassified
Component: Java (show other bugs)
Version: unspecified
Hardware: PC Windows
: --- enhancement
Assignee: keithlam5
URL:
Depends on:
Blocks:
 
Reported: 2023-11-09 23:51 HKT by keithlam5
Modified: 2023-11-27 14:55 HKT (History)
1 user (show)

See Also:


Attachments
Bug_1 (14.24 KB, image/png)
2023-11-09 23:51 HKT, keithlam5
Details

Note You need to log in before you can comment on or make changes to this bug.
Description keithlam5 2023-11-09 23:51:14 HKT
Created attachment 147 [details]
Bug_1

Steps To Reproduce:

1. startNewDay without any date


Actual Results: java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
	at code.CmdStartNewDay.execute(CmdStartNewDay.java:13)
	at Main.main(Main.java:45)

Expected Results: A warning of no date or wrong syntax
Comment 1 keithlam5 2023-11-27 14:55:51 HKT
Created an extra ExInvalidDate.java to handle the exception of invalid date input.

package code;

public class ExInvalidDate extends Exception {

    private String day;

    public String getDay() { return day; }

    public ExInvalidDate() { super("Invalid date."); }

    public ExInvalidDate(String message) { super(message); }
    
}


--------------
In the CmdStartNewDay.java. Changed the public void execute(String[] cmdParts) as follow:

    public void execute(String[] cmdParts) {
        try {

            SystemDate sd = SystemDate.getInstance();
            sOld = sd.toString();
            sNew = cmdParts[1];

            if (!sd.valid(sNew)) throw new ExInvalidDate();

            sd.set(sNew);

            addUndoCommand(this);
            clearRedoList();
            System.out.println("Done.");

        } catch (ExInvalidNewDate e) {
            System.out.println(e.getMessage());

        } catch (ExInvalidDate | ArrayIndexOutOfBoundsException | NumberFormatException e) {
			System.out.println(new ExInvalidDate().getMessage());
		}
    }