How can I skip null items in list?

Hello everyone, this one has me scratching my head. I have a list of XRSocketInteractors, and what I am trying to do is check to see if any of the sockets in the list have something in them. Lets say I have 4 spots to place an item, each one of those spots is in the socket list, and if I have something in the first slot, it works. But if I put something in any other one, I get a null exception error. Here’s some sudo code that follows what I’m doing.

public List<GameObject> attachPoints = new List<GameObject>();

public List<XRSocketInteractor> sockets = new List<XRSocketInteractor>();


 void Start()
    {
        for (int i = 0; i < attachPoints.Count; i++)
        {
            XRSocketInteractor socket = attachPoints*.GetComponent<XRSocketInteractor>();*

sockets.Add(socket);
}
}

public void ConnectedToPoint()
{
foreach (var socket in sockets)
{
if(socket.selectTarget.gameObject == null)
{
continue;
}
Debug.Log(socket.selectTarget.gameObject);
}
}
Keep in mind that’s not my real code, but it follows what I am trying to do. I call the code on the OnSelectEnter() of the XRSocketInteractor if that helps. Any help is welcomed, thank you guys!

Why not just remove all the null values before you loop through the list? Look at RemoveAll. Just before you start the loop do a var noNullList = sockets.RemoveAll(x => x is null); and then loop through noNullList.

You’d get that error if the socket was null, or if it wasn’t but socket.selectTarget was. A hammer&tongs way is hand-checking those:

if(socket==null) continue;  // leave this out if sockets can't be null
if(socket.select==null) continue;
// at this point, we know socket.selected is safe to use