Skip to content

Comments

fix: resolve NullReferenceException in postLoad() - fixes #56#59

Open
formeo wants to merge 1 commit intoMarvinBeym:masterfrom
formeo:fix/issue-56-nullref-postload-fix
Open

fix: resolve NullReferenceException in postLoad() - fixes #56#59
formeo wants to merge 1 commit intoMarvinBeym:masterfrom
formeo:fix/issue-56-nullref-postload-fix

Conversation

@formeo
Copy link

@formeo formeo commented Jan 21, 2026

Bugfix Summary for Issue #56

Problem Description

The MscModApi module was crashing with a NullReferenceException in postLoad() method, causing the mod to be disabled completely.

Error Message:

Error: The object reference is not set for the object instance in Void postLoad()
Error: The MscModApi module is disabled! Because it gives out too many errors in every frame!

Root Cause Analysis

The main issue was in ReplacedGamePartsDelayedInitializer.cs where several objects were accessed without null checks:

  1. FsmVariables.GlobalVariables.FindFsmBool("PlayerStop") - The PlayMaker FSM variables might not be initialized when PostLoad() is called
  2. ReplacedGameParts.modsParts - Could be null if LoadCleanup() wasn't called or failed
  3. replacedGamePartsDelayedInitializer - Could be null if PreLoad() wasn't executed

Files Modified

1. Source code/MscModApi/Parts/ReplacePart/ReplacedGamePartsDelayedInitializer.cs

Changes:

  • ✅ Added null check for ReplacedGameParts.modsParts in initializingRequired property
  • ✅ Added null check for FsmVariables.GlobalVariables before calling FindFsmBool()
  • ✅ Added null check for playerStop before accessing .Value
  • ✅ Added null check for MscModApi.disableLoadingMovementLock setting
  • ✅ Added null checks when iterating over modsParts collections
  • ✅ Added null check for events before calling InvokeAll()
  • ✅ Wrapped coroutine start in try-catch for better error handling
  • ✅ Added warning messages instead of crashing when FsmBool is not found

2. Source code/MscModApi/MscModApi.cs

Changes:

  • ✅ Added null check for replacedGamePartsDelayedInitializer in PostLoad()
  • ✅ Added warning message when the initializer is null

3. Source code/MscModApi/Parts/ReplacePart/ReplacedGameParts.cs

Changes:

  • ✅ Added null check for modsParts in Save() method
  • ✅ Added null checks for modParts.Value, mod, replacedGameParts, and originalPart
  • ✅ Skip iterations gracefully instead of throwing exceptions

4. Source code/MscModApi/Caching/Cache.cs

Changes:

  • ✅ Added defensive initialization of cachedGameObjects if null
  • ✅ Changed dictionary access to use TryGetValue instead of direct indexer

Testing Recommendations

  1. Test mod loading with a fresh game save
  2. Test mod loading with an existing save
  3. Test mod loading when other mods are also installed
  4. Test the "Disable movement lock while loading" option
  5. Verify no errors appear in the console during loading

Breaking Changes

None. All changes are backward compatible and add defensive programming without changing the API.

Additional Improvements Made

  1. Better error messages with [MscModApi] prefix for easier debugging
  2. Graceful degradation - the mod will now warn instead of crash when non-critical features fail
  3. Fixed a typo in error message ('{replacedGameParts.id}'{replacedGameParts.id}')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant