Fix magn5096: AstFactory methods require null checks

This pull request is to fix [MAGN 5096: AstFactory methods require null checks] (https://github.com/DynamoDS/Dynamo/pull/2277/files#r17060403)

@junmendoza please help to review the change. Thanks.