Skip to content

Added null Protection for getAcademy #6532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions MekHQ/src/mekhq/campaign/Campaign.java
Original file line number Diff line number Diff line change
Expand Up @@ -5670,6 +5670,11 @@ private void processEducationNewDay() {
if (EducationController.processNewDay(this, person, false)) {
Academy academy = getAcademy(person.getEduAcademySet(), person.getEduAcademyNameInSet());

if (academy == null) {
logger.debug("Found null academy for {} skipping", person.getFullTitle());
continue;
}

graduatingPersonnel.add(person.getId());

individualAcademyAttributes.add(academy.getEducationLevel(person));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.util.UUID;

import megamek.common.Compute;
import megamek.common.annotations.Nullable;
import megamek.logging.MMLogger;
import mekhq.MekHQ;
import mekhq.campaign.Campaign;
Expand Down Expand Up @@ -512,6 +513,11 @@ public static boolean processNewDay(Campaign campaign, Person person, boolean ag
ResourceBundle resources = ResourceBundle.getBundle(BUNDLE_NAME, MekHQ.getMHQOptions().getLocale());
Academy academy = getAcademy(person.getEduAcademySet(), person.getEduAcademyNameInSet());

if (academy == null) {
logger.debug("Found null academy for {} skipping", person.getFullTitle());
return false;
}

EducationStage educationStage = person.getEduEducationStage();

// is person in transit to the institution?
Expand Down Expand Up @@ -706,7 +712,7 @@ private static void processJourneyHome(Campaign campaign, Person person) {
*
* @return The Academy that matches the provided parameters or null if no match is found.
*/
public static Academy getAcademy(String academySetName, String academyNameInSet) {
public static @Nullable Academy getAcademy(String academySetName, String academyNameInSet) {

List<String> setNames = AcademyFactory.getInstance().getAllSetNames();

Expand Down
150 changes: 83 additions & 67 deletions MekHQ/src/mekhq/gui/adapter/PersonnelTableMouseAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,11 @@ public void actionPerformed(ActionEvent action) {
for (Person person : people) {
Academy academy = getAcademy(person.getEduAcademySet(), person.getEduAcademyNameInSet());

if (academy == null) {
logger.debug("Found null academy for {} skipping", person.getFullTitle());
continue;
}

EducationStage educationStage = person.getEduEducationStage();

switch (educationStage) {
Expand Down Expand Up @@ -538,6 +543,11 @@ public void actionPerformed(ActionEvent action) {
for (Person person : people) {
Academy academy = getAcademy(person.getEduAcademySet(), person.getEduAcademyNameInSet());

if (academy == null) {
logger.debug("Found null academy for {} skipping", person.getFullTitle());
continue;
}

EducationStage educationStage = person.getEduEducationStage();

switch (educationStage) {
Expand Down Expand Up @@ -2081,79 +2091,85 @@ protected Optional<JPopupMenu> createPopupMenu() {
if ((oneSelected) && (StaticChecks.areAllStudents(selected))) {
Academy academy = getAcademy(person.getEduAcademySet(), person.getEduAcademyNameInSet());

// this pile of if-statements just checks that the individual is eligible for
// re-enrollment
// has the person finished their education, but not yet returned to the unit?
if ((!person.getEduEducationStage().isJourneyToCampus()) &&
(!person.getEduEducationStage().isEducation())) {
// is the academy still standing?
if ((campaign.getGameYear() < academy.getDestructionYear()) &&
(campaign.getGameYear() < academy.getClosureYear())) {
// if the academy is local, is the system still populated?
if ((!academy.isLocal()) ||
(campaign.getCurrentSystem().getPopulation(campaign.getLocalDate()) > 0)) {
// is the person still within the correct age band?
if ((person.getAge(campaign.getLocalDate()) < academy.getAgeMax()) &&
(person.getAge(campaign.getLocalDate()) >= academy.getAgeMin())) {
// has the person been edited at some point and is no longer qualified?
if (academy.isQualified(person)) {
// here we check that the person will benefit from re-enrollment
int improvementPossible = 0;

String filteredFaction = academy.getFilteredFaction(campaign,
person,
List.of(person.getEduAcademyFaction()));

if (filteredFaction != null) {
int educationLevel = academy.getEducationLevel(person);

String[] skills = academy.getCurriculums()
.get(person.getEduCourseIndex())
.split(",");

skills = Arrays.stream(skills).map(String::trim).toArray(String[]::new);

for (String skill : skills) {
if (skill.equalsIgnoreCase("none")) {
continue;
}
if (academy == null) {
logger.debug("Found null academy for {} skipping", person.getFullTitle());
} else {
// this pile of if-statements just checks that the individual is eligible for
// re-enrollment
// has the person finished their education, but not yet returned to the unit?
if ((!person.getEduEducationStage().isJourneyToCampus()) &&
(!person.getEduEducationStage().isEducation())) {
// is the academy still standing?
if ((campaign.getGameYear() < academy.getDestructionYear()) &&
(campaign.getGameYear() < academy.getClosureYear())) {
// if the academy is local, is the system still populated?
if ((!academy.isLocal()) ||
(campaign.getCurrentSystem().getPopulation(campaign.getLocalDate()) > 0)) {
// is the person still within the correct age band?
if ((person.getAge(campaign.getLocalDate()) < academy.getAgeMax()) &&
(person.getAge(campaign.getLocalDate()) >= academy.getAgeMin())) {
// has the person been edited at some point and is no longer qualified?
if (academy.isQualified(person)) {
// here we check that the person will benefit from re-enrollment
int improvementPossible = 0;

String filteredFaction = academy.getFilteredFaction(campaign,
person,
List.of(person.getEduAcademyFaction()));

if (filteredFaction != null) {
int educationLevel = academy.getEducationLevel(person);

String[] skills = academy.getCurriculums()
.get(person.getEduCourseIndex())
.split(",");

skills = Arrays.stream(skills).map(String::trim).toArray(String[]::new);

for (String skill : skills) {
if (skill.equalsIgnoreCase("none")) {
continue;
}

if (skill.equalsIgnoreCase("xp")) {
if (EducationLevel.parseToInt(person.getEduHighestEducation()) <
educationLevel) {
improvementPossible++;
if (skill.equalsIgnoreCase("xp")) {
if (EducationLevel.parseToInt(person.getEduHighestEducation()) <
educationLevel) {
improvementPossible++;
}
} else {
String skillParsed = skillParser(skill);

if ((person.hasSkill(skillParsed)) &&
(person.getSkill(skillParsed).getExperienceLevel() <
educationLevel)) {
improvementPossible++;
} else if (!person.hasSkill(skillParsed)) {
improvementPossible++;
}
}
}

JMenuItem reEnroll;

if (improvementPossible > 0) {
reEnroll = new JMenuItem(resources.getString("eduReEnroll.text"));
reEnroll.setToolTipText(resources.getString("eduReEnroll.toolTip"));
reEnroll.setActionCommand(makeCommand(CMD_BEGIN_EDUCATION_RE_ENROLLMENT,
academy.getSet(),
academy.getName(),
String.valueOf(person.getEduCourseIndex()),
person.getEduAcademySystem(),
person.getEduAcademyFaction()));
reEnroll.addActionListener(this);
} else {
String skillParsed = skillParser(skill);

if ((person.hasSkill(skillParsed)) &&
(person.getSkill(skillParsed).getExperienceLevel() <
educationLevel)) {
improvementPossible++;
} else if (!person.hasSkill(skillParsed)) {
improvementPossible++;
}
reEnroll = new JMenuItem(resources.getString(
"eduReEnrollImpossible.text"));
reEnroll.setToolTipText(resources.getString(
"eduReEnrollImpossible.toolTip"));
}
}

JMenuItem reEnroll;

if (improvementPossible > 0) {
reEnroll = new JMenuItem(resources.getString("eduReEnroll.text"));
reEnroll.setToolTipText(resources.getString("eduReEnroll.toolTip"));
reEnroll.setActionCommand(makeCommand(CMD_BEGIN_EDUCATION_RE_ENROLLMENT,
academy.getSet(),
academy.getName(),
String.valueOf(person.getEduCourseIndex()),
person.getEduAcademySystem(),
person.getEduAcademyFaction()));
reEnroll.addActionListener(this);
} else {
reEnroll = new JMenuItem(resources.getString("eduReEnrollImpossible.text"));
reEnroll.setToolTipText(resources.getString("eduReEnrollImpossible.toolTip"));
academyMenu.add(reEnroll);
}

academyMenu.add(reEnroll);
}
}
}
Expand Down
Loading
Loading