Search Issue Tracker
By Design
Votes
0
Found in
2022.3.2f1
2023.1.0f1
2023.2.0a19
Issue ID
UUM-40349
Regression
Yes
NullReferenceException thrown when creating a List View
Reproduction steps:
1. Open the attached project "bug-binding-error"
2. Open "Window > ListView Custom Item"
Expected result: no NullReferenceException in the Console
Actual result: NullReferenceException thrown in the Console
Reproducible with: 2022.2.0a9, 2022.3.2f1, 2023.1.0f1, 2023.2.0a19
Not reproducible with: 2021.3.27f1, 2022.2.0a8
Reproducible on: macOS 13.2.1 (Intel), macOS 13.4 (M1)
Add comment
All about bugs
View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.
Latest issues
- [UIBuilder] Changes aren't detected on types implementing IList
- [HDRP] Text is rendered with artefacts when using STP and resolution scale is set a float value
- [Settings Search] Search results not always matching search term correctly
- Crash on memcpy when opening a project that loads TSS files
- Multiple errors appear when compiling a ShaderGraph shader's code in a specific project
Resolution Note:
This is working as designed. The developer defines the makeItem() callback function that returns a VisualElement to host the list view items as they are added to the list. The developer also defines a bindItem() callback function that binds the data from its source to the item that was created. But the problem here is that the makeItem() function also declares that it wants to be notified of value changes via RegisterValueChangedCallback() and when called, uses the objectField's userData property directly and casts to an integer. While normally this would work, the first time it is called (at the time of the exception in this example), the userData property is not yet initialized, so trying to cast null to an integer will fail. If the developer really wants to call this without initializing userData first, they can use the following code in their bindItem callback instead:
Action<VisualElement, int> bindItem = (e, i) =>
{
// instead of using ".value = ...", use ".SetValueWithoutNotify(...)"
(e as ObjectField).SetValueWithoutNotify(cellInfo.roomInfo.overrideRoomPrefabs[i]);
e.userData = i;
};
This will ensure that the assignment when the item is bound will not send a notification (so the registered value changed callback in makeItem won't be called).