Page 1 of 1

'Object reference' error when using string methods

Posted: Tue Feb 19, 2013 9:49 pm
by hporter
Hi, the test I am writing uses a While loop to query the state of a label on the webpage. If the InnerText of the label contains a particular string, the While loop exits. The trouble is, I'm getting an 'Object reference' error whenever I try to invoke the appropriate string method. Here is the code in question:

bool boolFailure = false;
string tempString = null;
while (!tempString.Contains("Processing has completed"))
{
tempString = repo.ExecuteSavedInstructions.ExecXML_status.InnerText;
if (tempString.Contains("Errors"))
{

Report.Failure("The following error occurred. " +tempString);
boolFailure = true;
break;
}
Thread.Sleep(500);
}
}

When I try to run the test, I get the following error:

Object reference not set to an instance of an object.
Show/Hide Stacktrace
at Test_Discovery.Recording1.Validate_DiscSuccess() in c:\HP RX tests\Test_Discovery\Test_Discovery\Recording1.UserCode.cs:line 44 at Test_Discovery.Recording1.Ranorex.Core.Testing.ITestModule.Run() in c:\HP RX tests\Test_Discovery\Test_Discovery\Recording1.cs:line 81 at Ranorex.Core.Testing.TestSuiteModule.RunInternal(DataContext parentDataContext)

Please note that line 44 equates to the line with the While statement. Also note that I've tried using other string methods (e.g. string.EndsWith), and the same error occurs. It's extremely important that I be able to use these methods. please advise. Thanks!

Re: 'Object reference' error when using string methods

Posted: Wed Feb 20, 2013 12:45 am
by Ciege
try changing
string tempString = null;
to
string tempString = repo.ExecuteSavedInstructions.ExecXML_status.InnerText;

Re: 'Object reference' error when using string methods

Posted: Wed Feb 20, 2013 2:14 am
by hporter
Thank you for the quick response, as always! No effect with that change, unfortunately; the error still occurs. Actually, that's how it was written initially, but thought perhaps that not first assigning the variable a null value was causing it, hence setting it to null first.

The code seems so basic, I'm very suspicious that there's something else going on. Perhaps a bug in Ranorex?

Re: 'Object reference' error when using string methods

Posted: Wed Feb 20, 2013 9:58 am
by Support Team
hporter wrote:The code seems so basic, I'm very suspicious that there's something else going on. Perhaps a bug in Ranorex?
No, sorry, this is not a Ranorex bug - even basic code can have bugs :D

As Ciege already posted, your code will immediately throw an exception, because you are trying to invoke the method "Contains" on the variable "tempString" that is initialized with "null". "null" means that there is no object assigned to that variable and that's why you cannot invoke a method on that object (because there is no object). You will get a NullReferenceException (this is almost always a coding problem...)
hporter wrote:tempString = repo.ExecuteSavedInstructions.ExecXML_status.InnerText;
Make sure to always check for "null" before invoking a method on an object. The "InnerText" property in this line can also return "null", then you will get the same NullReferenceException again.

I recommend reading a bit about coding in C# in general and the NullReferenceException in particular:
http://www.dotnetperls.com/nullreferenceexception
http://msdn.microsoft.com/en-us/library ... ption.aspx

Regards,
Alex
Ranorex Team

Re: 'Object reference' error when using string methods

Posted: Wed Feb 20, 2013 4:08 pm
by Ciege
Of course Alex is right, as always...

You can also set your string value to "" when you declare it.
string tempString = "";

Then put your entire loop into a try/catch block that will handle any Ranorex exceptions, such as element not found or a null. Also, be sure you add in a timer so that you don't wait forever for the expected text to appear.

Finally, once you get this working, you can variablize the element and the expected string and make this a reusable method to check for and wait for the expected innertext of any element. Then you can add this to a framework DLL and reuse the same method again and again throughout your different tests...

Re: 'Object reference' error when using string methods

Posted: Wed Feb 20, 2013 4:56 pm
by hporter
Thank you all so much for the assistance! Your information was most helpful. What I believe was happening was, when I was making my initial check on InnerText, at that exact moment it was still set to null, so it broke. I've added a check where, if InnerText is set to null, it does a short thread.sleep. Once it contains a legit value, it moves along and the test completes ultimately w/no errors. Much appreciated!

Ciege, your proposal makes a lot of sense. As is painfully obvious, I am not much of a programmer at this point :) I have a distant background in C++ (10 years or more ago), VB, and some C# here and there. What you describe is currently outside of my skill level, but hopefully not for long. Thanks again!