3 Replies - 546 Views - Last Post: 13 June 2013 - 05:50 AM Rate Topic: -----

#1 EllieAlz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-June 13

System.NullReferenceException was caught

Posted 13 June 2013 - 05:08 AM

Trying to automate a ComboBox in Microsoft Visual, however encountering a problem with this bit of code.
Message = Object reference not set to an instance of an object.
How do I fix this?


private void selectItem(AutomationElement theCB, string x)

        {

            try

            {

                LogMessage("Searching for " + x + " ...");

                AutomationElement appElement2 = theCB.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.NameProperty, x));  //USE SUBTREE -- not Children or Descendants

                if (appElement2 != null)

                {

                    LogMessage("Found " + p + " !");

                    SelectionItemPattern selectPattern = appElement2.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern;

                    selectPattern.Select();

                }

            }

            catch (Exception ex)

            {

               LogMessage(ex.Message + Environment.NewLine + ex.StackTrace);

            }

        }

This post has been edited by jimblumberg: 13 June 2013 - 05:34 AM
Reason for edit:: Added missing code tags. Please learn to use them properly.


Is This A Good Question/Topic? 0
  • +

Replies To: System.NullReferenceException was caught

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4142
  • View blog
  • Posts: 12,897
  • Joined: 25-December 09

Re: System.NullReferenceException was caught

Posted 13 June 2013 - 05:35 AM

First start using code tags when posting code.

Second what language are you trying to use?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3623
  • View blog
  • Posts: 11,291
  • Joined: 05-May 12

Re: System.NullReferenceException was caught

Posted 13 June 2013 - 05:42 AM

That code is actually in C#, not C++. Moving...

Where is the crash? Line 11 or Line 21?
Was This Post Helpful? 0
  • +
  • -

#4 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 362
  • View blog
  • Posts: 1,534
  • Joined: 08-April 09

Re: System.NullReferenceException was caught

Posted 13 June 2013 - 05:50 AM

Usually when i encounter this error it means that you forget to create instance of an object or you are trying to access a property of an object that is null

This post has been edited by Michael26: 13 June 2013 - 05:51 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1