You are making a call to the Revit API from a context that is not a valid Revit API context.
You can only call the Revit API from within a Revit event handler such as an external command Execute or an external event Execute method:
http://thebuildingcoder.typepad.com/blog/about-the-author.html#5.28
Best regards,
Jeremy