1

Hi I am using this code to read datagridview cell.Its working fine till

       var myString = targetText.Cached.Name;

targettext is null

Code:

       LogMessage("Getting RootElement...");
        AutomationElement rootElement = AutomationElement.RootElement;
        if (rootElement != null)
        {
            LogMessage("OK." + Environment.NewLine);

            Automation.Condition condition = new PropertyCondition(AutomationElement.NameProperty, "Form1");

            LogMessage("Searching for Test Window...");
            AutomationElement appElement = rootElement.FindFirst(TreeScope.Children, condition);

            if (appElement != null)
            {
                LogMessage("OK " + Environment.NewLine);
                LogMessage("Searching for Gridview control...");
                AutomationElement txtElementA = GetTextElement(appElement, "dg");
                var rows = txtElementA.FindAll(TreeScope.Children, PropertyCondition.TrueCondition);
                foreach (AutomationElement loginLine in rows)
                {
                    var loginLinesDetails = loginLine.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Custom));

                    for (var i = 0; i < loginLinesDetails.Count; i++)
                    {
                        var cacheRequest = new CacheRequest
                        {
                            AutomationElementMode = AutomationElementMode.None,
                            TreeFilter = System.Windows.Automation.Automation.RawViewCondition
                        };

                        cacheRequest.Add(AutomationElement.NameProperty);
                        cacheRequest.Add(AutomationElement.AutomationIdProperty);

                        cacheRequest.Push();

                        var targetText = loginLinesDetails[i].FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "TextBlock"));// targettext is null

                        cacheRequest.Pop();

                        var myString = targetText.Cached.Name;//Object reference not set ///to instance of an object error
                    }

                }

            }
            else
            {
                WriteLogError();
            }
        }

Here is an image of an example form i am trying to read at present.If it works for this small app it will definately work for big apps.

enter image description here

All I want to do is read the cells of a datagridview.I dont know whether I am doing right or not can any one please help me with this. There would be great appreciation if someone could help me

Ishaq Gi
  • 103
  • 8
  • 1
    it isnt a duplicate please read the question once again not the heading – Ishaq Gi Jan 16 '14 at 11:48
  • Have you tried checking if targettext is null? If its null you should not access it, it will give you Object reference not set... error. I think what akshay has suggested makes sense – ViSu Jan 16 '14 at 11:52
  • 1
    yeah i am getting null on that line.how can i get a value in that line inspite of null.Am i doing something wrong – Ishaq Gi Jan 16 '14 at 11:53
  • @IshaqGi It's a duplicate because the problem we're talking about is the same of the linked question. Debug the line where you have the null reference. The line `loginLinesDetails[i].FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "TextBlock"));` is returning you a null object. Check that line. – Alberto Solano Jan 16 '14 at 11:56
  • 1
    yes you are right I am getting a null value at that line.what should i do to get the cell value in that line instead of null value? – Ishaq Gi Jan 16 '14 at 11:59
  • @IshaqGi: If the object is `null` then there *is no* value. `null` is the very definition of a lack of a value. – David Jan 16 '14 at 13:17

2 Answers2

3

Before you try to assign value to

var myString = targetText.Cached.Name;

Check following condition

if(targetText != null && targetText.Cached!=null)
   var myString = targetText.Cached.Name;

Might be the case that following line is returning NULL

loginLinesDetails[i].FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "TextBlock"));
Akshay
  • 1,831
  • 1
  • 18
  • 22
  • I have updated my question can you please have a look at it sorry i forgot to mention that targettext is null – Ishaq Gi Jan 16 '14 at 11:47
  • @ Ishaq Gi That is why you getting exception. As you are trying to access values in NULL. like null.Cached.Name. – Akshay Jan 16 '14 at 11:50
  • yeah i am getting null on that line.how can i get a value in that line inspite of null.Am i doing something wrong Akshay ? – Ishaq Gi Jan 16 '14 at 11:50
  • there is'nt anything such as loginLinesDetails[i].FirstOrDefault in the reference.how did you get that ? – Ishaq Gi Jan 16 '14 at 11:57
  • I just updated the answer. Please have a look. – Akshay Jan 16 '14 at 11:59
  • no you havent updated the answer @Akshay have a glance at it – Ishaq Gi Jan 16 '14 at 12:02
  • 2
    this is a valid answer to this question. Handle the null or examine the line returning the null value. As for me this answer is valid. – no9 Jan 16 '14 at 12:11
  • but why i am getting an null value i've updated my question with a picture of a form which i want to read as an example. – Ishaq Gi Jan 16 '14 at 12:14
1

The problem might be that you are looking for a TextBox control, as suggested by the line

LogMessage("Searching for TextBox A control...");

but in the PropertyCondition you use "TextBlock" as class name.

Then you should write this:

var targetText = loginLinesDetails[i].FindFirst(TreeScope.Children,
    new PropertyCondition(AutomationElement.ClassNameProperty, "TextBox")); // here
Clemens
  • 123,504
  • 12
  • 155
  • 268