How to properly check serializedProperty.objectReferenceValue for null?

I’m writing on an editor methods for deleting items from a list.

The list looks as follows

[Serializedfield]
private List<TextItem> _textItems;

public class Item<T>
{
    public string Id;
    public List<T> Contents = new List<T>();

    public Item(string id, List<T> cont)
    {
        Id = id;
        Contents = cont;
    }
}

[Serializable]
public class TextItem : Item<string>
{
    public TextItem(string id, List<string> cont) : base(id, cont)
    {
    }
}

I saw this question/answer and implemented it like this within my CustomEditor class:

private void DeleteText(int index)
{
    SerializedProperty textList = serializedObject.FindProperty("_textItems");

    if (textList.GetArrayElementAtIndex(index).objectReferenceValue != null)
    {
        textList.DeleteArrayElementAtIndex(index);
    }

    textList.DeleteArrayElementAtIndex(index);
}

It is basically working but I always get an error thrown

type is not a supported pptr value
UnityEditor.SerializedProperty:get_objectReferenceValue()

pointing to the line where I check for null.

I also saw this question/answer and intempted to do the type casting like

if(textList.GetArrayElementAt(index).objectReferenceValue as System.Object as TextItem != null)

but without effect.

How can I get rid of this exception?

After some trial and error I’ve found a way to do it without much hassle:

if (someProperty.propertyType == SerializedPropertyType.ObjectReference ) {
    Debug.Log(someProperty.objectReferenceValue);  // will either be null or the reference, will never throw error
}

This will happen when your property doesn’t actually have an objectReferenceValue. For example, if you’re property is a UnityEvent.

Try tagging every inner type, and generic type used by T, with [System.Serializable].

 [Serializedfield]
 private List<TextItem> _textItems;

**[System.Serializable]**
 public class Item<T>
 {
     public string Id;
     public List<T> Contents = new List<T>();
     public Item(string id, List<T> cont)
     {
         Id = id;
         Contents = cont;
     }
 }

 [Serializable]
 public class TextItem : Item<string>
 {
     public TextItem(string id, List<string> cont) : base(id, cont)
     {
     }
 }