Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Add item footer crash in listview

Discussion in 'UI Toolkit' started by rod_lopez, Jul 5, 2022.

  1. rod_lopez

    rod_lopez

    Joined:
    Aug 10, 2017
    Posts:
    20
    Hi there!
    So my question is really very simple.
    I have two .uxml files, one for a frame (with a listView) and one for an item (that is to be added to the listView)
    I have added a add/remove footer to that listview.

    Things work As You'd Expect™ in Editor mode (so if my class is derived from EditorWindow), I click '+' and a new item pops in, life is good.
    But if I create a UIDocument in the scene and follow exactly the same procedure I get a crash when pressing either '+' or '-'. I imagine I need to implement something (that is implemented for me in the Editor version, but can't figure out what it is)

    The callstack is:
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. UnityEngine.UIElements.ListView.AddItems (System.Int32 itemCount) (at /Users/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/Controls/ListView.cs:383)
    3. UnityEngine.UIElements.ListView.OnAddClicked () (at /Users/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/Controls/ListView.cs:447)
    4. UnityEngine.UIElements.Clickable.Invoke (UnityEngine.UIElements.EventBase evt) (at /Users/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/Clickable.cs:292)
    5. UnityEngine.UIElements.Clickable.ProcessUpEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.Vector2 localPosition, System.Int32 pointerId) (at /Users/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/Clickable.cs:380)
    6. UnityEngine.UIElements.Clickable.OnMouseUp (UnityEngine.UIElements.MouseUpEvent evt) (at /Users/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/Clickable.cs:173)
    7. UnityEngine.UIElements.EventCallbackFunctor`1[TEventType].Invoke (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.PropagationPhase propagationPhase) (at /Users/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/Events/EventCallback.cs:79)
    8. UnityEngine.UIElements.EventCallbackRegistry.InvokeCallbacks (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.PropagationPhase propagationPhase) (at /Users/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/Events/EventCallbackRegistry.cs:353)
    9. UnityEngine.UIElements.CallbackEventHandler.HandleEvent (UnityEngine.UIElements.EventBase evt) (at /Users/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/Events/EventHandler.cs:186)
    10. UnityEngine.UIElements.TextElement.HandleEvent (UnityEngine.UIElements.EventBase evt) (at /Users/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/TextElement.cs:101)
    11. UnityEngine.UIElements.EventDispatchUtilities.PropagateEvent (UnityEngine.UIElements.EventBase evt) (at /Users/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/Events/IEventDispatchingStrategy.cs:113)
    Heeeelp? :D
     
  2. rod_lopez

    rod_lopez

    Joined:
    Aug 10, 2017
    Posts:
    20
    Okay, it was my own bloody fault. When moving my code from the editor mode I kept using
    Code (CSharp):
    1. var visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Main.uxml");
    2.  
    Instead of using
    Code (CSharp):
    1. var rootVisualElement = GetComponent<UIDocument>().rootVisualElement;
    Of course then the bind/make items callbacks where not set and that probably is why the inner code ended up with the NullRefernceException

    ...it would have helped to have the error message being a tad clearer (or even clickable to go the code that crashed), "users/bokken/buildslave" is definitely not part of my packageCache structure :)