FAQ: Why am I getting a NullReferencecException when calling a method on a UITestControl?


Most probably, this is because you have not initialized Playback.

 

In this post, Gautam has described how to use UI Testing methods without creating a Coded UI Test. It is important to note that,  If Playback.Initialize() is not called before invoking any UI Testing method, an exception will be thrown.

e.g:-

  • If you attempt to call UITestControl.Find() method (without calling Playback.Initialize), you will get a NullReferenceException. Note that there is an implicit Find() call whenever we act on a UI Test Control (unless it has previously been found and is available in the cache).
  • If you attempt to call BrowserWindow.Launch() method (without calling Playback.Initialize), you will get a TechnologyNotSupportedException

 

In a Coded UI Test, Playback.Initialize() and Playback.Cleanup() are called implicitly.

Comments (1)

  1. Melissa says:

    Hi Mathew, I have a same error when i run a test method. But the method is recorded by Code UI Builder totally. The weird thing is, I record the same method in 3 solutions, only one has this error.

    And I add the Playback.Initialize() in MyTestInitialize(), It can help nothing at all .

    Error Message:

    Test method Microsoft.Mtps.Test.SubscriptionsCUIT.Test.CodedUITest1.CodedUITestMethod1 threw exception:

    System.NullReferenceException: Object reference not set to an instance of an object.

    Error Stack Trace:

    Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetPropertyValue(String propertyName)

    Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetProperty(String propertyName)

    Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetPropertyInternal[T](String propertyName)

    Microsoft.VisualStudio.TestTools.UITesting.UITestControl.get_BoundingRectangle()

    Microsoft.VisualStudio.TestTools.UITesting.UITestControl.CaptureImage()

    Microsoft.VisualStudio.TestTools.UITesting.Playback.CaptureScreenShot(UITestControl control)

    Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetProperty(String propertyName)

    Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetPropertyInternal[T](String propertyName)

    Microsoft.VisualStudio.TestTools.UITesting.UITestControl.get_WindowHandle()

    Microsoft.VisualStudio.TestTools.UITesting.BrowserWindow.get_BrowserService()

    Microsoft.VisualStudio.TestTools.UITesting.BrowserWindow.NavigateToUrl(Uri uri)

    Microsoft.Mtps.Test.SubscriptionsCUIT.Test.UIMap.NavigateToSubscriberDownloads() in E:MTPSBranchesMainTestsSubscriptionsSubscriptionsCUITTestUIMap.Designer.cs: line 43

    Microsoft.Mtps.Test.SubscriptionsCUIT.Test.CodedUITest1.CodedUITestMethod1() in E:MTPSBranchesMainTestsSubscriptionsSubscriptionsCUITTestCodedUITest1.cs: line 29

Skip to main content