AaronGullickson / mekhq

MekHQ is a java helper program for the MegaMek game that allows users to load a list of entities from an XML file, perform repairs and customizations, and then save the new entities to another XML file that can be loaded into MegaMek.
http://megamek.info/mekhq
2 stars 0 forks source link

Bug: NPE error preventing story arcs from progressing #20

Closed AaronGullickson closed 7 months ago

AaronGullickson commented 8 months ago

I have noticed that ocassionally, I am not having story arcs progress after the conclusion of a scenario and I get the following in the log:

java.lang.NullPointerException
    at java.base/java.util.Objects.requireNonNull(Objects.java:222)
    at mekhq.campaign.event.ScenarioEvent.<init>(ScenarioEvent.java:36)
    at mekhq.campaign.event.ScenarioChangedEvent.<init>(ScenarioChangedEvent.java:31)
    at mekhq.campaign.event.ScenarioResolvedEvent.<init>(ScenarioResolvedEvent.java:31)
    at mekhq.MekHQ.gameVictory(MekHQ.java:501)
    at megamek.common.event.GameVictoryEvent.fireEvent(GameVictoryEvent.java:65)
    at megamek.common.Game.processGameEvent(Game.java:2998)
    at megamek.client.Client.handlePacket(Client.java:1703)
    at megamek.client.Client$1.lambda$packetReceived$0(Client.java:176)
    at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

Most recently, I noticed this with the Defend Ulric scenario on Wotan. However, it doesn't seem to happen consistently.

AaronGullickson commented 8 months ago

Ok, I think whats going on here is because of this in MekHQ.gameVictory:

gameThread.requestStop();
...
MekHQ.triggerEvent(new ScenarioResolvedEvent(currentScenario));

The GameThread.requestStop() method calls MekHQ.stopHost() which is, as far as I can tell, the only place that the currentScenario object is set to NULL. Because that thread is called before the ScenarioResolvedEven is triggered, the thread might have some possibility of nulling out currentScenario before the event is triggered. I don't think it happens every time (or even most of the time) because of a threaded nature, but does happen sometimes which explains the seeming randomness.

I believe the way to fix this will be to put the event triggering before the thread is stopped. Off the top of my head, I don't think this will have any other consequences, but I need to do some more checking and testing to be sure.

AaronGullickson commented 7 months ago

I ran through this in the debugger and that does appear to be the source of the problem. Moving the trigger immediately before the thread does not cause any problems, so I will do that.

AaronGullickson commented 7 months ago

Fixed in 3370fe29ee.