Object reference not set to an instance of an object

Class library usage, coding and language questions.
houseofcutler
Posts: 52
Joined: Fri Mar 21, 2014 4:22 pm

Object reference not set to an instance of an object

Post by houseofcutler » Fri May 09, 2014 4:15 pm

hi all,

I have been trying to resolve this issue for a while - I thought I had it (Some times the script runs through successfully) but apparently I don't.

I believe the issue is because I am trying to use a contains method with a string value which may at some point be null.

I found an old post about this and have followed the advice but still sometimes get an error.
http://www.ranorex.com/forum/object-ref ... t4384.html

The Item in question is a SMSStatus.TagValue

I have implemented a series of wait loops to try and ensure things can be processed.

1 - wait for item to exist
while (repo.GroupcallMessengerRoot.MessageDetails.Summary.SMSStatusInfo.Exists() == false)
        		
        	{
        		Thread.Sleep(500);
        		if (LoopLimiter <=0) break;
        		//Report.Info("Status",LoopLimiter.ToString());
        			LoopLimiter--;
2 - wait for item to be visible (not strictly necessary but helps me with debugging)
while (!repo.GroupcallMessengerRoot.MessageDetails.Summary.SMSStatus.Visible)
        		
        	{
        		Thread.Sleep(500);
        		if (LoopLimiter <=0) break;
        			LoopLimiter--;
3 - wait for tagValue to not be null
while (repo.GroupcallMessengerRoot.MessageDetails.Summary.SMSStatus.TagValue.Equals(null))
        		{
        		Thread.Sleep(500);
        		if (LoopLimiter <=0) break;
        		//Report.Info("TagValue",LoopLimiter.ToString());
        			LoopLimiter--;
There are also reports along the way so I know it is getting as far as the check for null value before it falls over.

Looking at the screenshot that is taken when it fails I do not see any text displayed in the area where the text form this item should be - That could be an indication that it is indeed null. if so why isn't my 3rd wait loop catching it?

As always any help is greatly appreciated

Thanks

Ben

krstcs
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Object reference not set to an instance of an object

Post by krstcs » Fri May 09, 2014 5:06 pm

You can't use the Equals() method with a null. String.Equals() takes a String object as a parameter, null is not an object (and is, in fact, the absence of an object, or, more specifically, the absence of a reference to an object).

If you want to test for a null object, you can use "...SMSStatus.TagValue == null" or "...SMSStatus.TagValue != null" (not equal). If you want to check for an empty string, you should use Equals("").
Shortcuts usually aren't...

houseofcutler
Posts: 52
Joined: Fri Mar 21, 2014 4:22 pm

Re: Object reference not set to an instance of an object

Post by houseofcutler » Tue May 13, 2014 3:54 pm

Hi krstcs,

Thank you for proving the answer - seems I was being a bit thick. I'm new to C# and still haven't got the hand of what kind of way to deal with different types of things.

Looks to be working now... :)