NullReferenceException when click on PropertyField with value in custom Editor Inspector

Hi!
I have a custom inspector (UnityEditor.Editor) for my script, and I have a SerializedProperty that is referenced by the owner of the editor.
This field works correctly in the inspector, BUT when I click on this field I get NullReferenceException (at this point field has a value, at least visually).
Why is it happening? and how to fix it?

Code examples:

public class TestClass : MonoBehaviour
{
   public Transform TestTranform;
}
[CustomEditor(typeof(TestClass))]
internal class TestClassEditor : UnityEditor.Editor
{
    SerializedProperty TestTranformProperty;
    private void OnEnable()
    {
        TestTranformProperty = serializedObject.FindProperty("TestTranform");
    }
 
   public override void OnInspectorGUI()
   {
      EditorGUILayout.PropertyField(TestTranformProperty);
      serializedObject.ApplyModifiedProperties();
   }
}

NullReferenceException

NullReferenceException: Object reference not set to an instance of an object
UnityEditor.ProjectBrowser.FrameObject (System.Int32 instanceID, System.Boolean ping) (at <83719566e1474cd8800c02cd178db127>:0)
UnityEditor.EditorGUIUtility.PingObject (System.Int32 targetInstanceID) (at <83719566e1474cd8800c02cd178db127>:0)
UnityEditor.EditorGUIUtility.PingObject (UnityEngine.Object obj) (at <83719566e1474cd8800c02cd178db127>:0)
UnityEditor.EditorGUI.PingObjectOrShowPreviewOnClick (UnityEngine.Object targetObject, UnityEngine.Rect position) (at <83719566e1474cd8800c02cd178db127>:0)
UnityEditor.EditorGUI.DoObjectField (UnityEngine.Rect position, UnityEngine.Rect dropRect, System.Int32 id, UnityEngine.Object obj, UnityEngine.Object objBeingEdited, System.Type objType, System.Type additionalType, UnityEditor.SerializedProperty property, UnityEditor.EditorGUI+ObjectFieldValidator validator, System.Boolean allowSceneObjects, UnityEngine.GUIStyle style, UnityEngine.GUIStyle buttonStyle, System.Action`1[T] onObjectSelectorClosed, System.Action`1[T] onObjectSelectedUpdated) (at <83719566e1474cd8800c02cd178db127>:0)
UnityEditor.EditorGUI.DoObjectField (UnityEngine.Rect position, UnityEngine.Rect dropRect, System.Int32 id, UnityEngine.Object obj, UnityEngine.Object objBeingEdited, System.Type objType, UnityEditor.SerializedProperty property, UnityEditor.EditorGUI+ObjectFieldValidator validator, System.Boolean allowSceneObjects, UnityEngine.GUIStyle style) (at <83719566e1474cd8800c02cd178db127>:0)
UnityEditor.EditorGUI.DoObjectField (UnityEngine.Rect position, UnityEngine.Rect dropRect, System.Int32 id, System.Type objType, UnityEditor.SerializedProperty property, UnityEditor.EditorGUI+ObjectFieldValidator validator, System.Boolean allowSceneObjects, UnityEngine.GUIStyle style) (at <83719566e1474cd8800c02cd178db127>:0)
UnityEditor.EditorGUI.ObjectFieldInternal (UnityEngine.Rect position, UnityEditor.SerializedProperty property, System.Type objType, UnityEngine.GUIContent label, UnityEngine.GUIStyle style, UnityEditor.EditorGUI+ObjectFieldValidator validator) (at <83719566e1474cd8800c02cd178db127>:0)
UnityEditor.EditorGUI.DefaultPropertyField (UnityEngine.Rect position, UnityEditor.SerializedProperty property, UnityEngine.GUIContent label) (at <83719566e1474cd8800c02cd178db127>:0)
UnityEditor.PropertyHandler.OnGUI (UnityEngine.Rect position, UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, System.Boolean includeChildren, UnityEngine.Rect visibleArea) (at <83719566e1474cd8800c02cd178db127>:0)
UnityEditor.PropertyHandler.OnGUI (UnityEngine.Rect position, UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, System.Boolean includeChildren) (at <83719566e1474cd8800c02cd178db127>:0)
UnityEditor.PropertyHandler.OnGUILayout (UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, System.Boolean includeChildren, UnityEngine.GUILayoutOption[] options) (at <83719566e1474cd8800c02cd178db127>:0)
UnityEditor.EditorGUILayout.PropertyField (UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, System.Boolean includeChildren, UnityEngine.GUILayoutOption[] options) (at <83719566e1474cd8800c02cd178db127>:0)
UnityEditor.EditorGUILayout.PropertyField (UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, UnityEngine.GUILayoutOption[] options) (at <83719566e1474cd8800c02cd178db127>:0)
TestClassEditor.OnInspectorGUI () (at Assets/TestClassEditor.cs:309)
UnityEditor.UIElements.InspectorElement+<>c__DisplayClass71_0.<CreateIMGUIInspectorFromEditor>b__0 () (at <83719566e1474cd8800c02cd178db127>:0)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)

Unity version 2022.2.3f1

Try it with this:

public override void OnInspectorGUI()
{
    serializedObject.Update();

    EditorGUILayout.PropertyField(TestTranformProperty);

    serializedObject.ApplyModifiedProperties();
}

Thanks for reply! This helped me!

@MelvMay , Sorry, I was wrong that it helped.
I closed the scene and reopen then I tried to click on the object reference in the inspector and it really works.
BUT, if you changed something in any code and Unity recompile it, you will get NullRefException again.
It happened with and without:

serializedObject.Update();

You can reopen scene and it will work correctly, but it is enough to recompile the code - the error reappears.

Are you sure you are not violating the assumptions of Unity references??

The most common one and easiest to violate is the rule that any on-disk asset (eg, prefab, ScriptableObject, etc.) can NEVER have a reference to something in the scene.

This is obviously because scenes come and go from memory, so nothing stored on disk could ever point to them.

In this situation, I do not use resources from disk, I only use a reference to the Transform class from the object that is on the scene.

The code is quite simple, the first post contains a complete case to reproduce this issue.
Enough:

  1. add these 2 classes (TestClass, TestClassEditor)
  2. move the GameObject from the scene to the inspector
  3. recompile the code (changing something and saving, going to the Unity, it will recompile)
  4. click on the object in the inspector - there will be a NullReferenceException

And, if you reopen the scene, everything will work correctly, BUT if you repeate 3 and 4 points (above), the exception will appear again.

Up