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

2 posts, 0 answers
  1. Siddi
    Siddi avatar
    3 posts
    Member since:
    Jun 2013

    Posted 27 Jun 2013 Link to this post

    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;

                }

            }

        }


  2. Velin Koychev
    Admin
    Velin Koychev avatar
    88 posts

    Posted 28 Jun 2013 Link to this post

    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!
Back to Top