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!
'Object reference' error when using string methods
Re: 'Object reference' error when using string methods
try changing
string tempString = null;
to
string tempString = repo.ExecuteSavedInstructions.ExecXML_status.InnerText;
string tempString = null;
to
string tempString = repo.ExecuteSavedInstructions.ExecXML_status.InnerText;
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!
Ciege...
Ciege...
Re: 'Object reference' error when using string methods
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?
The code seems so basic, I'm very suspicious that there's something else going on. Perhaps a bug in Ranorex?
- Support Team
- Site Admin
- Posts: 12145
- Joined: Fri Jul 07, 2006 4:30 pm
- Location: Houston, Texas, USA
- Contact:
Re: 'Object reference' error when using string methods
No, sorry, this is not a Ranorex bug - even basic code can have bugshporter wrote:The code seems so basic, I'm very suspicious that there's something else going on. Perhaps a bug in Ranorex?
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...)
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.hporter wrote:tempString = repo.ExecuteSavedInstructions.ExecXML_status.InnerText;
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
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...
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...
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!
Ciege...
Ciege...
Re: 'Object reference' error when using string methods
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!
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!