Bug 696 - Unhandled exception type IOException when running Main.java
Summary: Unhandled exception type IOException when running Main.java
Status: RESOLVED FIXED
Alias: None
Product: River-Crossing-Puzzle-Solver
Classification: Unclassified
Component: Solver Mode (show other bugs)
Version: v0.9.5
Hardware: PC Mac OS
: Normal major
Assignee: tsunychau2-c
URL:
Depends on:
Blocks:
 
Reported: 2022-11-05 21:55 HKT by tsunychau2-c
Modified: 2022-11-05 21:56 HKT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description tsunychau2-c 2022-11-05 21:55:01 HKT
Way to Reproduce:

1. Run Main.java inside the folder Default

2. Input "1" in the console for choosing Solver mode

3. Input "1" in the console for choosing game level

Actual Result:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Unhandled exception type IOException

	at Puzzle.Puzzle.<init>(Puzzle.java:64)
	at Puzzle.UI.SolverModeController.<init>(SolverModeController.java:14)
	at Puzzle.UI.SolverMode.<init>(SolverMode.java:31)
	at Puzzle.UI.GameFlowFactory.getGameFlow(GameFlowFactory.java:6)
	at Puzzle.UI.Main.main(Main.java:34)
	at Default.Main.main(Main.java:5)

Expect Result:

No error is shown
Comment 1 tsunychau2-c 2022-11-05 21:55:14 HKT
Bug is confirmed
Comment 2 tsunychau2-c 2022-11-05 21:56:40 HKT
Inside Puzzle.java, Puzzle constructor should surround with "try and catch" to prevent the error shown.

Modification of code:

public Puzzle(String path) {
		JSONObject jsonObj = null;
		
		try {
			Path file = Path.of(path);
			String obj = Files.readString(file);
			jsonObj = new JSONObject(obj);

			// store the JSON obj, for other classes to access
			this.puzzleJSON = jsonObj;

			name = (String)jsonObj.get("PuzzleName");
			description = (String) jsonObj.get("Description");
			roleList = jsonArray2arrayList((JSONArray) jsonObj.get("Roles"));

			JSONArray jsonArray;
			jsonArray = (JSONArray) jsonObj.get("InitialState");
			initialState = new PuzzleState(jsonArray2arrayList((JSONArray) jsonArray.get(0)),
					jsonArray2arrayList((JSONArray) jsonArray.get(1)), 0);
			jsonArray = (JSONArray) jsonObj.get("TargetState");
			targetState = new PuzzleState(jsonArray2arrayList((JSONArray) jsonArray.get(0)),
					jsonArray2arrayList((JSONArray) jsonArray.get(1)), 0);

			travelables = new HashSet<String>(jsonArray2arrayList((JSONArray) jsonObj.get("Travelable")));
		} catch (Exception e) {
			System.out.println(e.getMessage());
	}
	
		
	}