Skip to content

Commit 8901b32

Browse files
authored
Merge pull request #6428 from IllianiCBT/scenarioResolutionIllegalArgumentExceptionTrigger
Added Null Checks To Prevent Potential Runtime Exceptions During Scenario Resolution
2 parents cdff6b5 + 3b1a166 commit 8901b32

File tree

1 file changed

+51
-22
lines changed

1 file changed

+51
-22
lines changed

Diff for: MekHQ/src/mekhq/MekHQ.java

+51-22
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.io.ObjectInputFilter.Config;
4141
import java.lang.management.ManagementFactory;
4242
import java.util.List;
43+
import java.util.Map;
4344
import java.util.UUID;
4445
import javax.swing.InputMap;
4546
import javax.swing.JOptionPane;
@@ -108,8 +109,8 @@ public class MekHQ implements GameListener {
108109

109110
// region Variable Declarations
110111
private static final SuitePreferences mhqPreferences = new SuitePreferences();
111-
private static final MHQOptions mhqOptions = new MHQOptions();
112-
private static final EventBus EVENT_BUS = new EventBus();
112+
private static final MHQOptions mhqOptions = new MHQOptions();
113+
private static final EventBus EVENT_BUS = new EventBus();
113114

114115
private static ObservableString selectedTheme;
115116

@@ -124,21 +125,21 @@ public class MekHQ implements GameListener {
124125
private static ObservableString financesDirectory;
125126

126127
// stuff related to MM games
127-
private Server myServer = null;
128-
private GameThread gameThread = null;
129-
private Scenario currentScenario = null;
130-
private Client client = null;
128+
private Server myServer = null;
129+
private GameThread gameThread = null;
130+
private Scenario currentScenario = null;
131+
private Client client = null;
131132

132133
// the actual campaign - this is where the good stuff is
133134
private CampaignController campaignController;
134-
private CampaignGUI campaignGUI;
135+
private CampaignGUI campaignGUI;
135136

136137
private final IconPackage iconPackage = new IconPackage();
137138

138-
private final IAutosaveService autosaveService;
139+
private final IAutosaveService autosaveService;
139140
// endregion Variable Declarations
140141
private static final SanityInputFilter sanityInputFilter = new SanityInputFilter();
141-
private static final String defaultTheme = "com.formdev.flatlaf.FlatDarculaLaf";
142+
private static final String defaultTheme = "com.formdev.flatlaf.FlatDarculaLaf";
142143

143144
public static SuitePreferences getMHQPreferences() {
144145
return mhqPreferences;
@@ -318,9 +319,9 @@ public static void main(String... args) {
318319

319320
// First, create a global default exception handler
320321
Thread.setDefaultUncaughtExceptionHandler((thread, t) -> {
321-
final String name = t.getClass().getName();
322+
final String name = t.getClass().getName();
322323
final String message = String.format(MMLoggingConstants.UNHANDLED_EXCEPTION, name);
323-
final String title = String.format(MMLoggingConstants.UNHANDLED_EXCEPTION_TITLE, name);
324+
final String title = String.format(MMLoggingConstants.UNHANDLED_EXCEPTION_TITLE, name);
324325
logger.errorDialog(t, message, title);
325326
});
326327

@@ -390,9 +391,9 @@ public void joinGame(Scenario scenario, List<Unit> meks) {
390391
return;
391392
}
392393

393-
final String playerName = joinGameDialog.getPlayerName();
394+
final String playerName = joinGameDialog.getPlayerName();
394395
final String serverAddress = joinGameDialog.getServerAddress();
395-
final int port = joinGameDialog.getPort();
396+
final int port = joinGameDialog.getPort();
396397
joinGameDialog.dispose();
397398

398399
try {
@@ -443,11 +444,11 @@ public void startHost(Scenario scenario, boolean loadSavegame, List<Unit> meks,
443444

444445
this.autosaveService.requestBeforeMissionAutosave(getCampaign());
445446

446-
final String playerName = hostDialog.getPlayerName();
447-
final String password = hostDialog.getServerPass();
448-
final int port = hostDialog.getPort();
449-
final boolean register = hostDialog.isRegister();
450-
final String metaserver = register ? hostDialog.getMetaserver() : "";
447+
final String playerName = hostDialog.getPlayerName();
448+
final String password = hostDialog.getServerPass();
449+
final int port = hostDialog.getPort();
450+
final boolean register = hostDialog.isRegister();
451+
final String metaserver = register ? hostDialog.getMetaserver() : "";
451452

452453
// Force cleanup of the current modal, since we are (possibly) about to display a new one and macOS seems to
453454
// struggle with that (see https://github.com/MegaMek/mekhq/issues/953)
@@ -583,7 +584,7 @@ public void gameVictory(PostGameResolution gve) {
583584
BattlefieldControlType battlefieldControl = template.getBattlefieldControl();
584585

585586
String controlMessage = MHQInternationalization.getText("ResolveDialog.control." +
586-
battlefieldControl.name());
587+
battlefieldControl.name());
587588

588589
victoryMessage = String.format("%s\n\n%s", controlMessage, victoryMessage);
589590
}
@@ -632,7 +633,7 @@ public void resolveScenario(Scenario selectedScenario) {
632633
BattlefieldControlType battlefieldControl = template.getBattlefieldControl();
633634

634635
String controlMessage = MHQInternationalization.getText("ResolveDialog.control." +
635-
battlefieldControl.name());
636+
battlefieldControl.name());
636637

637638
victoryMessage = String.format("%s\n\n%s", controlMessage, victoryMessage);
638639
}
@@ -763,7 +764,7 @@ public void autoResolveConcluded(AutoResolveConcludedEvent autoResolveConcludedE
763764
BattlefieldControlType battlefieldControl = template.getBattlefieldControl();
764765

765766
String controlMessage = MHQInternationalization.getText("ResolveDialog.control." +
766-
battlefieldControl.name());
767+
battlefieldControl.name());
767768

768769
victoryMessage = String.format("%s\n\n%s\n\n%s", controlMessage, victoryMessage, decisionMessage);
769770
}
@@ -784,10 +785,38 @@ public void autoResolveConcluded(AutoResolveConcludedEvent autoResolveConcludedE
784785
true,
785786
tracker);
786787
resolveDialog.setVisible(true);
788+
// TODO remove these safeties as they're shown to be unnecessary
789+
if (resolveDialog == null) {
790+
throw new IllegalStateException("resolveDialog is null");
791+
}
787792
if (resolveDialog.wasAborted()) {
793+
// TODO remove these safeties as they're shown to be unnecessary
794+
Map<UUID, ?> peopleStatus = tracker.getPeopleStatus();
795+
if (peopleStatus == null) {
796+
throw new IllegalStateException("People status map in tracker is null");
797+
}
798+
788799
for (UUID personId : tracker.getPeopleStatus().keySet()) {
800+
// TODO remove these safeties as they're shown to be unnecessary
801+
if (getCampaign() == null) {
802+
throw new IllegalStateException("Campaign instance is null");
803+
}
804+
789805
Person person = getCampaign().getPerson(personId);
790-
person.setHits(person.getHitsPrior());
806+
807+
if (person == null) {
808+
throw new IllegalArgumentException("Person with ID " +
809+
personId +
810+
" does not exist in the campaign");
811+
}
812+
813+
Integer priorHits = person.getHitsPrior();
814+
// TODO remove these safeties as they're shown to be unnecessary
815+
if (priorHits == null) {
816+
throw new IllegalStateException("Person's prior hits are not set for person " +
817+
person.getFullName());
818+
}
819+
person.setHits(priorHits);
791820
}
792821
return;
793822
}

0 commit comments

Comments
 (0)