From 00cc07534a5c55920a1ca06514d7225daac8c179 Mon Sep 17 00:00:00 2001 From: IllianiCBT Date: Tue, 14 Jan 2025 19:58:16 -0600 Subject: [PATCH 1/2] Handle exception for out-of-bounds tab index selection Encapsulated tab index selection logic in a try-catch block to prevent crashes when the index is out of bounds. Ignored exceptions as the default behavior safely skips selection if the index is invalid. Added a comment to clarify this behavior. --- MekHQ/src/mekhq/gui/TOETab.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/MekHQ/src/mekhq/gui/TOETab.java b/MekHQ/src/mekhq/gui/TOETab.java index 6d2332aa1c..d782c440c5 100644 --- a/MekHQ/src/mekhq/gui/TOETab.java +++ b/MekHQ/src/mekhq/gui/TOETab.java @@ -175,8 +175,12 @@ public Dimension getPreferredScrollableViewportSize() { tabUnit.add("Unit", scrollUnit); panForceView.add(tabUnit, BorderLayout.CENTER); SwingUtilities.invokeLater(() -> scrollUnit.getVerticalScrollBar().setValue(0)); - tabUnit.setSelectedIndex(tabUnitLastSelectedIndex); - tabUnit.addChangeListener(evt -> forceViewTabChange()); // added late so it won't overwrite + try { + tabUnit.setSelectedIndex(tabUnitLastSelectedIndex); + tabUnit.addChangeListener(evt -> forceViewTabChange()); // added late so it won't overwrite + } catch (Exception ignored) {} + // We can ignore here because if the selected index is out of bounds, we're just going + // to not select the unit in the TO&E. } else if (node instanceof Force) { final JScrollPane scrollForce = new JScrollPaneWithSpeed(new ForceViewPanel((Force) node, getCampaign())); panForceView.add(scrollForce, BorderLayout.CENTER); From d766831ad0d83157796843605dde310d16f13de2 Mon Sep 17 00:00:00 2001 From: IllianiCBT Date: Tue, 14 Jan 2025 19:59:47 -0600 Subject: [PATCH 2/2] Handle specific exception in TOETab index selection Changed the catch block to handle ArrayIndexOutOfBoundsException instead of a generic Exception. This improves code clarity and ensures only relevant exceptions are handled in this context. --- MekHQ/src/mekhq/gui/TOETab.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MekHQ/src/mekhq/gui/TOETab.java b/MekHQ/src/mekhq/gui/TOETab.java index d782c440c5..87f92ee8e9 100644 --- a/MekHQ/src/mekhq/gui/TOETab.java +++ b/MekHQ/src/mekhq/gui/TOETab.java @@ -178,7 +178,7 @@ public Dimension getPreferredScrollableViewportSize() { try { tabUnit.setSelectedIndex(tabUnitLastSelectedIndex); tabUnit.addChangeListener(evt -> forceViewTabChange()); // added late so it won't overwrite - } catch (Exception ignored) {} + } catch (ArrayIndexOutOfBoundsException ignored) {} // We can ignore here because if the selected index is out of bounds, we're just going // to not select the unit in the TO&E. } else if (node instanceof Force) {