[Solved?] Object reference not set to an instance of an object

So I am getting this error in my code and it is really bugging me,l because there are literally no object references anywhere where it is pointing to.

Could it be because there is an ForEach inside a ForEach?

Edit: Removing .With(Group) made it stop, but Group is not null, so… why?
I really need that there

Here is the code:

using System;
using Guedez.Util;
using Unity.Entities;
using UnityEngine;

public abstract class ChangeMinMaxValueCapsBase<V, M, A, MM, T> : QuickSystemFilter5EntityShared<RecipeEffect, RecipeEffectActivated, MM, A, T, FinalItemReference, EffectCostAndValue> where V : struct, IComponentData, IAccessor<float> where M : struct, IComponentData, MinMax<float> {
    public EntityQuery results;
    public EntityQuery ingredients;

    public abstract EntityQuery Group { get; }
    public abstract Func<float, float, float> func { get; }
    public abstract Action<MinMax<float>, int, float> minaxfunc { get; }

    protected override void OnCreateManager() {
        base.OnCreateManager();
        results = GetEntityQuery(typeof(FinalItemReference), typeof(IsResult));
        ingredients = GetEntityQuery(typeof(FinalItemReference), ComponentType.Exclude<IsResult>());
    }
    protected override void OnUpdate() {
        Entities.With(m_MainGroup).ForEach((Entity e, FinalItemReference fref, ref EffectCostAndValue cost) => {
            M minmax = EntityManager.GetComponentData<M>(fref.result);
            float value = cost.value;
            DynamicBuffer<EffectTargetAttribute> cont = EntityManager.GetBuffer<EffectTargetAttribute>(e);
            Unity.Collections.NativeArray<byte> nativeArray = cont.Reinterpret<byte>().AsNativeArray();
            Utils.BreakPoint();
            foreach (byte b in nativeArray) {
                minaxfunc(minmax, b, value);
            }
            Group.SetFilter(fref);
            //Debug.Log(Group.ToString() + ", " + func.Method.Name + ", " + minmax + ", [" + string.Join(", ", nativeArray.ToArray()) + "]");
            //try {
                Entities.With(Group).ForEach((ref V v) => {
                    foreach (byte b in nativeArray) {
                        v[b] = func(v[b], value);
                    }
                });
            //} catch (Exception) {

            //}
            EntityManager.SetComponentData(fref.result, minmax);
            MultiplyForTags.BuildLogs(EntityManager, e, fref.result, fref);
            PostUpdateCommands.DestroyEntity(e);
        });
    }
    protected override void OnUpdate(Entity e, FinalItemReference s, ref EffectCostAndValue t1) {
        throw new NotImplementedException();
    }
}

public abstract class ChangeMinValueCapsBase<V, M, A, MM, T> : ChangeMinMaxValueCapsBase<V, M, A, MM, T> where V : struct, IComponentData, IAccessor<float> where M : struct, IComponentData, MinMax<float> {
    public override Func<float, float, float> func { get => Mathf.Max; }
    public override Action<MinMax<float>, int, float> minaxfunc { get => (M, I, V) => M.Min(I, func(M.Min(I), V)); }
}
public abstract class ChangeMaxValueCapsBase<V, M, A, MM, T> : ChangeMinMaxValueCapsBase<V, M, A, MM, T> where V : struct, IComponentData, IAccessor<float> where M : struct, IComponentData, MinMax<float> {
    public override Func<float, float, float> func { get => Mathf.Min; }
    public override Action<MinMax<float>, int, float> minaxfunc { get => (M, I, V) => M.Max(I, func(M.Max(I), V)); }
}

public abstract class ChangeMinIngredientsValueCapsBase<V, M, A, MM> : ChangeMinValueCapsBase<V, M, A, MM, EffectTargetAll> where V : struct, IComponentData, IAccessor<float> where M : struct, IComponentData, MinMax<float> {
    public override EntityQuery Group => ingredients;
}
public abstract class ChangeMaxIngredientsValueCapsBase<V, M, A, MM> : ChangeMaxValueCapsBase<V, M, A, MM, EffectTargetAll> where V : struct, IComponentData, IAccessor<float> where M : struct, IComponentData, MinMax<float> {
    public override EntityQuery Group => ingredients;
}
public abstract class ChangeMinResultsValueCapsBase<V, M, A, MM> : ChangeMinValueCapsBase<V, M, A, MM, EffectTargetResults> where V : struct, IComponentData, IAccessor<float> where M : struct, IComponentData, MinMax<float> {
    public override EntityQuery Group => results;
}
public abstract class ChangeMaxResultsValueCapsBase<V, M, A, MM> : ChangeMaxValueCapsBase<V, M, A, MM, EffectTargetResults> where V : struct, IComponentData, IAccessor<float> where M : struct, IComponentData, MinMax<float> {
    public override EntityQuery Group => results;
}

[UpdateInGroup(typeof(GroupEffectApplyEffects))]
public class ChangeMinCapsIngredientsNutrients : ChangeMinIngredientsValueCapsBase<NutrientProcessor.Nutrients, NutrientsMinMax, EffectsAffectsNutrients, RecipeEffectChangeCapMin> {
}
[UpdateInGroup(typeof(GroupEffectApplyEffects))]
public class ChangeMinCapsIngredientsTastes : ChangeMinIngredientsValueCapsBase<TasteProcessor.Tastes, TastesMinMax, EffectsAffectsTastes, RecipeEffectChangeCapMin> {
}
[UpdateInGroup(typeof(GroupEffectApplyEffects))]
public class ChangeMinCapsIngredientsAroma : ChangeMinIngredientsValueCapsBase<AromaProcessor.Aroma, AromaMinMax, EffectsAffectsAroma, RecipeEffectChangeCapMin> {
}
[UpdateInGroup(typeof(GroupEffectApplyEffects))]
public class ChangeMinCapsIngredientsQuality : ChangeMinIngredientsValueCapsBase<ItemInstance, QualityMinMax, EffectsAffectsQuality, RecipeEffectChangeCapMin> {
}
[UpdateInGroup(typeof(GroupEffectApplyEffects))]
public class ChangeMinCapsResultsNutrients : ChangeMinResultsValueCapsBase<NutrientProcessor.Nutrients, NutrientsMinMax, EffectsAffectsNutrients, RecipeEffectChangeCapMin> {
}
[UpdateInGroup(typeof(GroupEffectApplyEffects))]
public class ChangeMinCapsResultsTastes : ChangeMinResultsValueCapsBase<TasteProcessor.Tastes, TastesMinMax, EffectsAffectsTastes, RecipeEffectChangeCapMin> {
}
[UpdateInGroup(typeof(GroupEffectApplyEffects))]
public class ChangeMinCapsResultsAroma : ChangeMinResultsValueCapsBase<AromaProcessor.Aroma, AromaMinMax, EffectsAffectsAroma, RecipeEffectChangeCapMin> {
}
[UpdateInGroup(typeof(GroupEffectApplyEffects))]
public class ChangeMinCapsResultsQuality : ChangeMinResultsValueCapsBase<ItemInstance, QualityMinMax, EffectsAffectsQuality, RecipeEffectChangeCapMin> {
}
[UpdateInGroup(typeof(GroupEffectApplyEffects))]
public class ChangeMaxCapsIngredientsNutrients : ChangeMaxIngredientsValueCapsBase<NutrientProcessor.Nutrients, NutrientsMinMax, EffectsAffectsNutrients, RecipeEffectChangeCapMax> {
}
[UpdateInGroup(typeof(GroupEffectApplyEffects))]
public class ChangeMaxCapsIngredientsTastes : ChangeMaxIngredientsValueCapsBase<TasteProcessor.Tastes, TastesMinMax, EffectsAffectsTastes, RecipeEffectChangeCapMax> {
}
[UpdateInGroup(typeof(GroupEffectApplyEffects))]
public class ChangeMaxCapsIngredientsAroma : ChangeMaxIngredientsValueCapsBase<AromaProcessor.Aroma, AromaMinMax, EffectsAffectsAroma, RecipeEffectChangeCapMax> {
}
[UpdateInGroup(typeof(GroupEffectApplyEffects))]
public class ChangeMaxCapsIngredientsQuality : ChangeMaxIngredientsValueCapsBase<ItemInstance, QualityMinMax, EffectsAffectsQuality, RecipeEffectChangeCapMax> {
}
[UpdateInGroup(typeof(GroupEffectApplyEffects))]
public class ChangeMaxCapsResultsNutrients : ChangeMaxResultsValueCapsBase<NutrientProcessor.Nutrients, NutrientsMinMax, EffectsAffectsNutrients, RecipeEffectChangeCapMax> {
}
[UpdateInGroup(typeof(GroupEffectApplyEffects))]
public class ChangeMaxCapsResultsTastes : ChangeMaxResultsValueCapsBase<TasteProcessor.Tastes, TastesMinMax, EffectsAffectsTastes, RecipeEffectChangeCapMax> {
}
[UpdateInGroup(typeof(GroupEffectApplyEffects))]
public class ChangeMaxCapsResultsAroma : ChangeMaxResultsValueCapsBase<AromaProcessor.Aroma, AromaMinMax, EffectsAffectsAroma, RecipeEffectChangeCapMax> {
}
[UpdateInGroup(typeof(GroupEffectApplyEffects))]
public class ChangeMaxCapsResultsQuality : ChangeMaxResultsValueCapsBase<ItemInstance, QualityMinMax, EffectsAffectsQuality, RecipeEffectChangeCapMax> {
}

The error:

NullReferenceException: Object reference not set to an instance of an object
Unity.Collections.LowLevel.Unsafe.UnsafeUtility.CopyPtrToStructure[T] (System.Void* ptr, T& output) (at C:/buildslave/unity/build/Runtime/Export/Unsafe/UnsafeUtilityPatched.cs:15)
Unity.Entities.EntityQueryBuilder.ForEach[T0] (Unity.Entities.EntityQueryBuilder+F_D`1[T0] action) (at Library/PackageCache/com.unity.entities@0.1.1-preview/Unity.Entities/EntityQueryBuilder_ForEach.gen.cs:112)
ChangeMinMaxValueCapsBase`5[V,M,A,MM,T].<OnUpdate>b__9_0 (Unity.Entities.Entity e, FinalItemReference fref, EffectCostAndValue& cost) (at Assets/Everyday Engine/src/ECS/Cooking/IgredientEffects/Systems/ChangeValueCaps.cs:34)
Unity.Entities.EntityQueryBuilder.ForEach[T0,T1] (Unity.Entities.EntityQueryBuilder+F_ESD`2[T0,T1] action) (at Library/PackageCache/com.unity.entities@0.1.1-preview/Unity.Entities/EntityQueryBuilder_ForEach.gen.cs:2369)
ChangeMinMaxValueCapsBase`5[V,M,A,MM,T].OnUpdate () (at Assets/Everyday Engine/src/ECS/Cooking/IgredientEffects/Systems/ChangeValueCaps.cs:22)
Unity.Entities.ComponentSystem.InternalUpdate () (at Library/PackageCache/com.unity.entities@0.1.1-preview/Unity.Entities/ComponentSystem.cs:800)
Unity.Entities.ComponentSystemBase.Update () (at Library/PackageCache/com.unity.entities@0.1.1-preview/Unity.Entities/ComponentSystem.cs:284)
Unity.Entities.ComponentSystemGroup.OnUpdate () (at Library/PackageCache/com.unity.entities@0.1.1-preview/Unity.Entities/ComponentSystemGroup.cs:602)
UnityEngine.Debug:LogException(Exception)
Unity.Debug:LogException(Exception) (at Library/PackageCache/com.unity.entities@0.1.1-preview/Unity.Entities/Stubs/Unity/Debug.cs:25)
Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.entities@0.1.1-preview/Unity.Entities/ComponentSystemGroup.cs:606)
Unity.Entities.ComponentSystem:InternalUpdate() (at Library/PackageCache/com.unity.entities@0.1.1-preview/Unity.Entities/ComponentSystem.cs:800)
Unity.Entities.ComponentSystemBase:Update() (at Library/PackageCache/com.unity.entities@0.1.1-preview/Unity.Entities/ComponentSystem.cs:284)
Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.entities@0.1.1-preview/Unity.Entities/ComponentSystemGroup.cs:602)
Unity.Entities.ComponentSystem:InternalUpdate() (at Library/PackageCache/com.unity.entities@0.1.1-preview/Unity.Entities/ComponentSystem.cs:800)
Unity.Entities.ComponentSystemBase:Update() (at Library/PackageCache/com.unity.entities@0.1.1-preview/Unity.Entities/ComponentSystem.cs:284)
Unity.Entities.DummyDelegateWrapper:TriggerUpdate() (at Library/PackageCache/com.unity.entities@0.1.1-preview/Unity.Entities/ScriptBehaviourUpdateOrder.cs:144)

I actually stumbled on the answer on accident on another system I just made

This error happens when the filter group does not contains an entity component you ask for in ForEach
For instance
results = GetEntityQuery(typeof(FinalItemReference), typeof(IsResult))
Should be
results = GetEntityQuery(typeof(FinalItemReference), typeof(IsResult), typeof(V))
Why is this even a requisite, is a mystery to me, does a ForEach without an With(group) will generate a group based on the lambda?

Please change the error message to clarify what’s wrong with the code

1 Like