locked
Object Exists but get a null reference trying to interact with it. RRS feed

  • Question

  • Hi,

    I've written a very basic script which opens a browser, goes to google and enters some text in the search box [below]. The object is found and its .Exists property is true but when I try to set the text or access any of it's member methods I get a null reference...

    Code:

    BrowserWindow browser = Actions.LaunchBrowser();
    UITestControl qEdit = new UITestControl(browser);
    qEdit.TechnologyName = "Web";
    qEdit.SearchProperties.Add("HtmlEdit.PropertyNames.Title", "Google Search");
    qEdit.SearchProperties.Add("HtmlEdit.PropertyNames.Name", "q");
    
    Actions.NavigateTo(browser, "http://www.google.com");
    
    qEdit.Find()
    
    if (qEdit.Exists)
    {
    	qEdit.SetProperty(HtmlEdit.PropertyNames.Text, "test");
    	Rectangle rect = qEdit.BoundingRectangle;
    }

    Error:

    System.NullReferenceException: Object reference not set to an instance of an object.
      at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetPropertyValue(String propertyName)
      at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetProperty(String propertyName)
      at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetPropertyInternal[T](String propertyName)
      at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.get_BoundingRectangle()
      at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.CaptureImage()
      at Microsoft.VisualStudio.TestTools.UITesting.Playback.CaptureScreenShot(UITestControl control)
      at Microsoft.VisualStudio.TestTools.UITesting.Playback.GetUITestControlString(UITestControl control)
      at Microsoft.VisualStudio.TestTools.UITesting.Playback.AddUITestControlDescriptionToException(SystemException exception, IPlaybackContext context)
      at Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowException(SystemException exception, IPlaybackContext context)
      at Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowException(SystemException exception, String actionName, UITestControl uiControl)
      at Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowException(SystemException exception, String actionName, Object parameterValue, UITestControl uiControl)
      at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetPropertyValue(String propertyName)
      at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetProperty(String propertyName)
      at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetPropertyInternal[T](String propertyName)
      at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.get_ControlType()
      at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.SetProperty(String propertyName, Object value)
      at QA_Automation.TestCases.Demo.DemoTestCases.TestCase_00100() in c:\users\gabell\documents\visual studio 2010\Projects\QA_Automation\QA_Automation\TestCases\Demo\DemoTestCases.cs:line 70

    Graham

    Friday, June 25, 2010 1:03 PM

Answers

  • Graham,

    Ensure that the following assemblies added as reference with CopyLocal as False. To check the CopyLocal setting, select the assembly in references and hit F4 to see its properties.

    1. Microsoft.VisualStudio.TestTools.UITesting.dll

    2. Microsoft.VisualStudio.TestTools.UITest.Common.dll

    3. Microsoft.VisualStudio.TestTools.UITest.Extension.dll

    Tell me if the issue still persists.

    Vinoth.

    • Marked as answer by Graham Abell Monday, June 28, 2010 11:22 AM
    Monday, June 28, 2010 11:01 AM

All replies

  • Hi Graham, can you check if you made the following call

    qEdit.SetProperty("HtmlEdit.PropertyNames.Text", "test");

    instead of

    qEdit.SetProperty(HtmlEdit.PropertyNames.Text, "test");

    If so, please change to the second one and try again.

    Thanks,

    Neeraja

    Friday, June 25, 2010 3:55 PM
    Moderator
  • Hi Neeraja,

    I've tried both ways and get an error with each - the second way:

    System.NullReferenceException: Object reference not set to an instance of an object.
      at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetPropertyValue(String propertyName)
      at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetProperty(String propertyName)
      at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetPropertyInternal[T](String propertyName)
      at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.get_ControlType()
      at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.SetProperty(String propertyName, Object value)
      at QA_Automation.TestCases.Demo.DemoTestCases.TestCase_00100() in c:\users\gabell\documents\visual studio 2010\Projects\Ergo-QA_Automation\Ergo-QA_Automation\TestCases\Demo\DemoTestCases.cs:line 49

    As I mentioned, I can find() the control and the .Exists returns true but most other methods return this NullReferenceException, for example .BoundingRectangle(), .ClassName etc...

    I've tried purposely describing it incorrectly and it then is no longer found using the find() method.

     

    Thanks,

    Graha,

     

    Friday, June 25, 2010 4:17 PM
  • Hi Guys,

     

    Any other suggestions on this? It's a bit of a blocker for me! :)

     

    Thanks,

    Graham

     

    Monday, June 28, 2010 9:23 AM
  • Graham,

    Are you trying this with Coded UI Test or some other project type? Looks like few of the product assemblies are getting copied to the out folder since CopyLocal has been set to True(which is not the case with default Coded UI Test template).

    Vinoth.

    Monday, June 28, 2010 10:15 AM
  • Hi Vinoth, 

     

    I created a coded UI project and then created a test as per the instructions here:

    http://blogs.msdn.com/b/mathew_aniyan/archive/2009/02/12/hand-coding-a-coded-ui-test.aspx

     

    I suppose some of the setting may be incorrect?

     

    Thanks,

    Graham

    Monday, June 28, 2010 10:27 AM
  • Graham,

    Ensure that the following assemblies added as reference with CopyLocal as False. To check the CopyLocal setting, select the assembly in references and hit F4 to see its properties.

    1. Microsoft.VisualStudio.TestTools.UITesting.dll

    2. Microsoft.VisualStudio.TestTools.UITest.Common.dll

    3. Microsoft.VisualStudio.TestTools.UITest.Extension.dll

    Tell me if the issue still persists.

    Vinoth.

    • Marked as answer by Graham Abell Monday, June 28, 2010 11:22 AM
    Monday, June 28, 2010 11:01 AM
  • Hi Vinoth,

    All those had CopyLocal set to True, I've reset and the problem is gone.


    Out of interest can you tell me why that caused the problem?

    Thanks,
    Graham

    Monday, June 28, 2010 11:24 AM
  • Our playback extensibility framework specifically loads Microsoft.VisualStudio.TestTools.UITesting.dll from %VSINSTALLDIR%\Common7\IDE\PublicAssemblies. Now in your case, as CopyLocal was set to true, it was copied to the out folder and loaded from there too. A portion of the product code starts using assembly #1 and another portion assembly #2 causing conflicts.

    By the way, when you create a new test project, is CopyLocal for these assemblies always True? If that's the case, then there is a bug which we need to look at.

    Vinoth.

    Monday, June 28, 2010 11:49 AM
  • I created a new blank test project and the properties were false so I'm not sure why my original project was wrong, but it's a good one to look out for regardless.

     

    Thanks for the explanation.


    Monday, June 28, 2010 2:14 PM