This is a migrated thread and some comments may be shown as answers.

Nunit Template - "System.NullReferenceException:Object reference not set to an instance of an object" is displayed when calling method from one class to other

1 Answer 547 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Siddi
Top achievements
Rank 1
Siddi asked on 27 Jun 2013, 06:46 PM
Nunit Template - "System.NullReferenceException:Object reference not set to an instance of an object"  is displayed when calling method from one class to other

Error is displayed when Iam calling method from class 2 to class 1.

Please find below sample code

namespace UnitTestProject4

{

public class Class1: BaseTest

[Test]

    { public void Testmethod1()

        {

            Manager.LaunchNewBrowser(BrowserType.InternetExplorer);

            ActiveBrowser.NavigateTo("http://www.wikipedia.org/");

            Class2. Testmethod2 ("Telerik Test Studio");

            Find.ByName<HtmlInputSubmit>("go").Click();

        }

  public partial class Class2

        {

           static Settings setting = new Settings();

            static Manager Manager = new Manager(setting);

            public static void Testmethod2 (string A)

            {

             Manager.ActiveBrowser.Find.ById<HtmlInputSearch>("searchInput").Text = A;

            }

        }

    }


1 Answer, 1 is accepted

Sort by
0
Velin Koychev
Telerik team
answered on 28 Jun 2013, 01:41 PM
Hi Siddi,

You can fix this issue following these steps:
1) In Class2 you should have code similar to this:

public partial class Class2
    {
        public static void Testmethod2(string A)
        {
            Manager.Current.ActiveBrowser.Find.ById<HtmlInputSearch>("searchInput").Text = A;
        }
    }
 2) As you are using the Nunit Template, make sure that you do not delete the #region [Setup / TearDown] in Class1. So you should have in your Class1 code similar to this:
public class Class1 : BaseTest
   {
       [SetUp]
       public void MyTestInitialize()
       {
           Initialize(false, new TestContextWriteLine(Console.Out.WriteLine));
       }
       [TearDown]
       public void MyTestCleanUp()
       {
           this.CleanUp();
       }
       [TestFixtureTearDown]
       public void FixtureCleanup()
       {
           ShutDown();
       }
       [Test]
       public void Testmethod1()
       {
           Manager.LaunchNewBrowser(BrowserType.InternetExplorer);
 
           ActiveBrowser.NavigateTo("http://www.wikipedia.org/");
 
           Class2.Testmethod2("Telerik Test Studio");
 
           Find.ByName<HtmlInputSubmit>("go").Click();
       }
   }

I hope this helps.
Regards,
Velin Koychev
Telerik
Free summer webinars on advanced web automation tactics hosted by Jim Holmes & Adam Goucher.
Reserve your seat today!
Tags
General Discussions
Asked by
Siddi
Top achievements
Rank 1
Answers by
Velin Koychev
Telerik team
Share this question
or