voult2 / BetterPawnControl

A RimWorld mod that allows bulk assignment of animals to zones and colonists to outfits (or drugs) in one single action
MIT License
20 stars 21 forks source link

Animal tab doesn't display; error on click #20

Closed Daenyth closed 3 years ago

Daenyth commented 4 years ago

I'm not sure what happened, but my animal tab no longer displays (even though it used to work), and I get this error in my log:

Root level exception in OnGUI(): System.NullReferenceException: Object reference not set to an instance of an object
  at BetterPawnControl.AnimalManager+<>c__DisplayClass6_0.<UpdateState>b__0 (BetterPawnControl.AnimalLink x) [0x00000] in <7db7a9c0c7c842c8bbd28e7598605eba>:0 
  at System.Collections.Generic.List`1[T].FindAll (System.Predicate`1[T] match) [0x00013] in <567df3e0919241ba98db88bec4c6696f>:0 
  at BetterPawnControl.AnimalManager.UpdateState (System.Collections.Generic.List`1[T] links, System.Collections.Generic.List`1[T] pawns, BetterPawnControl.Policy policy) [0x0001f] in <7db7a9c0c7c842c8bbd28e7598605eba>:0 
  at BetterPawnControl.MainTabWindow_Animals_Policies.PreOpen () [0x00020] in <7db7a9c0c7c842c8bbd28e7598605eba>:0 
  at Verse.WindowStack.Add (Verse.Window window) [0x0001f] in <b9022531d79047b2b6baad3f7d519f01>:0 
  at (wrapper dynamic-method) RimWorld.MainTabsRoot.DMD<DMD<ToggleTab_Patch1>?66738048::ToggleTab_Patch1>(RimWorld.MainTabsRoot,RimWorld.MainButtonDef,bool)
  at RimWorld.MainButtonWorker_ToggleTab.Activate () [0x00005] in <b9022531d79047b2b6baad3f7d519f01>:0 
  at RimWorld.MainButtonWorker.InterfaceTryActivate () [0x0004d] in <b9022531d79047b2b6baad3f7d519f01>:0 
  at (wrapper dynamic-method) RimWorld.MainButtonWorker.DMD<DMD<DoButton_Patch0>?-1308741760::DoButton_Patch0>(RimWorld.MainButtonWorker,UnityEngine.Rect)
  at RimWorld.MainButtonsRoot.DoButtons () [0x0010c] in <b9022531d79047b2b6baad3f7d519f01>:0 
  at (wrapper dynamic-method) RimWorld.MainButtonsRoot.DMD<DMD<MainButtonsOnGUI_Patch1>?98404992::MainButtonsOnGUI_Patch1>(RimWorld.MainButtonsRoot)
  at RimWorld.UIRoot_Play.UIRootOnGUI () [0x00037] in <b9022531d79047b2b6baad3f7d519f01>:0 
  at (wrapper dynamic-method) Verse.Root.DMD<DMD<OnGUI_Patch1>?-1867099264::OnGUI_Patch1>(Verse.Root)
Verse.Log:Error(String, Boolean)
Verse.Root:DMD<DMD<OnGUI_Patch1>?-1867099264::OnGUI_Patch1>(Root)
Daenyth commented 4 years ago

I'm suspicious of https://github.com/voult2/BetterPawnControl/blob/master/Source/Managers/AnimalManager.cs#L145 - I think x.zone might be the null, because I have noticed that among all my animal mods I've managed to get some in state where no zone was selected before. I think this might be an interaction with the 'Area inclusion & exclusion' mod.

I'm able to use the Numbers mod animal tab as a workaround for now, I'll check that all animals have some assigned zone and if that makes the problem go away

Daenyth commented 4 years ago

It's not due to Area inclusion - I removed that to test, and the problem persists.

Daenyth commented 3 years ago

I cloned the project to open it in Rider, and looking at the source a bit more carefully, it seems like more likely the problem is that one of the AnimalLink values in the links argument is null.

If so, this might help; I'll try to figure out how to build and test on my world locally

-            //get all links from the current map
-            mapLinks = links.FindAll(x => x.mapId == currentMap);
-            //get all links from the selected zone
-            zoneLinks = mapLinks.FindAll(x => x.zone == policy.id);
+            //get all links from the current map
+            mapLinks = links.FindAll(x => x != null && x.mapId == currentMap);
+            //get all links from the selected zone
+            zoneLinks = mapLinks.FindAll(x => x != null && x.zone == policy.id);
Daenyth commented 3 years ago

OK, I was able to confirm that AnimalManager.links contains a null value by having PreOpen for the tab invoke PrintAllAnimalPolicies;

[BPC] ### List Animal links [67] ###
[BPC]   Policy:0  Animal:   Master:   Area:   FollowDrafted: False  FollowFieldwork: False  MapID: 0
[BPC]   Policy:0  Animal: Cow69680  Master:   Area:   FollowDrafted: False  FollowFieldwork: False  MapID: 0
[BPC]   Policy:1  Animal:   Master:   Area:   FollowDrafted: False  FollowFieldwork: False  MapID: 0
[BPC]   Policy:1  Animal: Cow69680  Master:   Area:   FollowDrafted: False  FollowFieldwork: False  MapID: 0
[BPC]   Policy:0  Animal: Dromedary29608  Master:   Area:   FollowDrafted: False  FollowFieldwork: False  MapID: 0
[BPC]   Policy:1  Animal: Dromedary29608  Master:   Area:   FollowDrafted: False  FollowFieldwork: False  MapID: 0
[BPC]   Policy:0  Animal:   Master:   Area:   FollowDrafted: False  FollowFieldwork: False  MapID: 0
[BPC]   Policy:0  Animal:   Master:   Area:   FollowDrafted: False  FollowFieldwork: False  MapID: 0
[BPC]   Policy:1  Animal:   Master:   Area:   FollowDrafted: False  FollowFieldwork: False  MapID: 0
[BPC]   Policy:1  Animal:   Master:   Area:   FollowDrafted: False  FollowFieldwork: False  MapID: 0
[BPC]   Policy:0  Animal:   Master:   Area: RimWorld.Area_Home  FollowDrafted: False  FollowFieldwork: False  MapID: 0
[BPC]   Policy:0  Animal:   Master:   Area:   FollowDrafted: False  FollowFieldwork: False  MapID: 0
[BPC]   Policy:0  Animal:   Master:   Area:   FollowDrafted: False  FollowFieldwork: False  MapID: 0
[BPC]   Policy:0  Animal:   Master:   Area:   FollowDrafted: False  FollowFieldwork: False  MapID: 0
[BPC]   Policy:0  Animal:   Master:   Area:   FollowDrafted: False  FollowFieldwork: False  MapID: 0
[BPC]   Policy:0  Animal: Cow151497  Master:   Area:   FollowDrafted: False  FollowFieldwork: False  MapID: 0
Root level exception in OnGUI(): System.NullReferenceException: Object reference not set to an instance of an object
  at BetterPawnControl.AnimalManager.PrintAllAnimalPolicies (System.String spacer) [0x00126] in <6c6ec8003e37446fbc185f6a5ee0140b>:0
  at BetterPawnControl.MainTabWindow_Animals_Policies.PreOpen () [0x0000e] in <6c6ec8003e37446fbc185f6a5ee0140b>:0
  at Verse.WindowStack.Add (Verse.Window window) [0x0001f] in <b9022531d79047b2b6baad3f7d519f01>:0
  at (wrapper dynamic-method) RimWorld.MainTabsRoot.DMD<DMD<ToggleTab_Patch1>?-2069195392::ToggleTab_Patch1>(RimWorld.MainTabsRoot,RimWorld.MainButtonDef,bool)
  at RimWorld.MainButtonWorker_ToggleTab.Activate () [0x00005] in <b9022531d79047b2b6baad3f7d519f01>:0
  at RimWorld.MainButtonWorker.InterfaceTryActivate () [0x0004d] in <b9022531d79047b2b6baad3f7d519f01>:0
  at (wrapper dynamic-method) RimWorld.MainButtonWorker.DMD<DMD<DoButton_Patch0>?-1203342208::DoButton_Patch0>(RimWorld.MainButtonWorker,UnityEngine.Rect)
  at RimWorld.MainButtonsRoot.DoButtons () [0x0010c] in <b9022531d79047b2b6baad3f7d519f01>:0
  at (wrapper dynamic-method) RimWorld.MainButtonsRoot.DMD<DMD<MainButtonsOnGUI_Patch1>?952941952::MainButtonsOnGUI_Patch1>(RimWorld.MainButtonsRoot)
  at RimWorld.UIRoot_Play.UIRootOnGUI () [0x00037] in <b9022531d79047b2b6baad3f7d519f01>:0
  at (wrapper dynamic-method) Verse.Root.DMD<DMD<OnGUI_Patch1>?1404324992::OnGUI_Patch1>(Verse.Root)