Sometimes "Object reference not set to an instance of object

Ask general questions here.
agroenen
Posts: 17
Joined: Thu Jun 09, 2016 3:18 pm

Sometimes "Object reference not set to an instance of object

Post by agroenen » Thu Mar 30, 2017 12:29 pm

I created usercode that can be used in a recording; it works, but sometimes when I change one of the parameters I get an "unexpected error" and build error "Object reference not set to an instance of an object".
When I restart Ranorex it works again.

The code gets a date (f.e. $varBouwdatum or 01-01-2017), add days to it, and types the new date:

Code: Select all

 
public static void addDays(string varDatum, int Days, Ranorex.Adapter K2WItem)
{
			// string to datetime:
			System.DateTime dt=System.DateTime.Parse(varDatum);
			// add days:
			dt = dt.AddDays(Days);
			// datetime to string:
			string dat = dt.ToString();
			// type new date:
			K2WItem.Click();
			Keyboard.Press(dat);} 
Code.JPG
Code.JPG (17.41 KiB) Viewed 916 times
I'm pretty new at Ranorex / C#, so I hope someone can help me with that errors...!

User avatar
Vega
Posts: 110
Joined: Mon Jul 11, 2016 2:15 pm

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

Post by Vega » Thu Mar 30, 2017 3:51 pm

Can you post your exact error / stacktrace? It should point you to the line in question. Which parameter are you changing to cause the error? I have a feeling it may be your third parameter as the adapter may not exist yet.

agroenen
Posts: 17
Joined: Thu Jun 09, 2016 3:18 pm

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

Post by agroenen » Thu Mar 30, 2017 4:16 pm

It seems to happen with a change in all of them. But not always...

First:
Unexpected error.JPG
Unexpected error.JPG (36.7 KiB) Viewed 899 times
Then, when I try to run it again:
Failed to update code for 'C:\Users\agroenen\Documents\Ranorex\RanorexStudio Projects\K2W\K2W_Desktop\Key2Wocas\TestAnoushka.rxrec'. Object reference not set to an instance of an object. - C:\Users\agroenen\Documents\Ranorex\RanorexStudio Projects\K2W\K2W_Desktop\Key2Wocas\TestAnoushka.rxrec

I don't see linenumbers in the Stacktrace:
Stacktrace.JPG
Stacktrace.JPG (79.98 KiB) Viewed 899 times

User avatar
Vega
Posts: 110
Joined: Mon Jul 11, 2016 2:15 pm

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

Post by Vega » Fri Mar 31, 2017 7:12 pm

There will be no specified line because the reference issue is referring to the recording file itself. What version of Ranorex are you on? I was able to run your code and change variables without issue. Have you tried making a new blank solution and testing your code there?

agroenen
Posts: 17
Joined: Thu Jun 09, 2016 3:18 pm

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

Post by agroenen » Mon Apr 03, 2017 9:59 am

We're using Ranorex 6.2.1.
Unfortunately I get the same errors in a clean solution; I runned it in a new recording.

User avatar
Vega
Posts: 110
Joined: Mon Jul 11, 2016 2:15 pm

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

Post by Vega » Tue Apr 04, 2017 2:59 pm

My guess is that part of your Ranorex install has become corrupted. I would recommend you reinstall Ranorex and if you keep having issues also reinstall your .NET framework.