4 Replies - 1469 Views - Last Post: 11 December 2014 - 05:35 AM Rate Topic: -----

#1 ken2010   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 58
  • Joined: 05-December 10

Null Reference exception

Posted 11 December 2014 - 02:39 AM

[TestClass]
    public class TestBase
    {
        public IWebDriver driver;
        public TestBase(IWebDriver driver)
        {
            try {
                this.driver = driver;
            }
            catch (Exception e) { e.Message.ToString(); }
            
        }

        public TestBase() { }
    }


[TestClass]
    public class TestHome : TestBase.TestBase
    {
        public TestHome(IWebDriver driver) : base(driver) { }

        public TestHome()
        {
            
        }

        [TestMethod]
        public void LoginSuccessful()
        {
            driver.FindElement(By.Id("txtUsername")).SendKeys("Admin");
            driver.FindElement(By.Id("txtPassword")).SendKeys("admin");
            driver.FindElement(By.Id("btnLogin")).Click();
        }
    }


[TestClass]
    public class TestRunner
    {
       public IWebDriver driver;
       public TestRunner()
        {
            driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("http://professional.demo.orangehrmlive.com");
        }

        [TestMethod]
       public void RunningTestcases() 
       {
           TestHome testHome = new TestHome();
           testHome.LoginSuccessful();
       }
    }


I have these 3 classes, when I make a call to run the TesRunner class to run the test it throws null reference exception. Showing that the driver class initialized is null, what can I do?

Is This A Good Question/Topic? 0
  • +

Replies To: Null Reference exception

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,228
  • Joined: 12-December 12

Re: Null Reference exception

Posted 11 December 2014 - 02:47 AM

You are asking for help, not promoting an Advanced Discussion. Topic moved to the C# forum.

Post the full error message, and confirm which line it refers to in your posted code.

    TestHome testHome = new TestHome();
    testHome.LoginSuccessful();

When you use the default, nullary, constructor TestHome() the driver isn't instantiated, and LoginSuccessful() refers to driver.
Was This Post Helpful? 1
  • +
  • -

#3 ken2010   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 58
  • Joined: 05-December 10

Re: Null Reference exception

Posted 11 December 2014 - 03:09 AM

Full exception report:
An exception of type 'System.NullReferenceException' occurred in SampleTestingProjectAppZone.dll but was not handled in user code
Additional information: Object reference not set to an instance of an object.

when i debug, it points to
driver.FindElement(By.Id(TCHome.username)).Clear();

This shows you are right. But i thought since it has inherited from the base class the driver class in there then i ought not to instanciate it again. Could you help me with the right line of code? Thanks.

This post has been edited by ken2010: 11 December 2014 - 03:14 AM

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,228
  • Joined: 12-December 12

Re: Null Reference exception

Posted 11 December 2014 - 03:26 AM

It appears that the driver is essential for your class so you could make the empty constructor private, or remove it, so that your class cannot be instantiated without providing the driver.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Null Reference exception

Posted 11 December 2014 - 05:35 AM

Null ref exception is the first error covered in the article "What does this error mean?" linked below

See FAQ # 2 below

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 6: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated July 2014
Spoiler

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1