The FindObjectSpaceByObject method returns null (Nothing in VB.NET) if the object specified by the obj parameter belongs to an XPO Session not managed by the ObjectSpace class. This can be the case, for example, if you create an object using a separate Session as follows.
Session theSession = new Session(theDataLayer);
MyObject theObject = new MyObject(theSession);
Dim theSession As New Session(theDataLayer)
Dim theObject As New MyObject(theSession)
Note that you can access certain Object Spaces used internally by XAF via the FindObjectSpaceByObject method. Such Object Spaces should not be used in your applications. One example of such an Object Space is the Object Space used by the Security System.
Using such code in your applications may lead to undesired side-effects. So, instead of using an internal Object Space, create a new Object Space and perform the required actions with it: