Object reference not set to an instance of an object

27,318

Solution 1

If you have an object in a class It needs to be instantiate before you can use it. Arguably one of the best places to do this is in you constructor.

like this:

public class class2
{
   IWebDriver driver = null;


   public class2(IWebDriver driver)
   {
      this.driver = driver;
   }
   public void method()
   {
     driver.Navigate().GoToUrl("http://www.yahoo.com/");
   }
}

and then your other class would look like this

public void test1()
 {
    driver = new FirefoxDriver();
    class2 obj = new class2(driver);

    driver.Navigate().GoToUrl("http://www.google.com/");
    obj.method();
 }

Solution 2

Look at your code:

public class class2
{
    IWebDriver driver = null;
    public void method()
    {
        driver.Navigate().GoToUrl("http://www.yahoo.com/");
    }
}

Of course you're getting a NullReferenceException - driver is always null.

It's not clear what you expected to happen here - but perhaps you meant to pass the FirefoxDriver you instantiate in test1 into method via a parameter?

Solution 3

You're assigning the driver in your Class1, so when it tries to navigate on class2's method it fails, as class2's driver is null. You need to assign it a value before calling any methods on it.

I don't know why you wouldn't expect it to fail with a NullReferenceException.

What you probably meant to write was:

  public class class2
  {
    public void method(IWebDriver driver)
    {
        driver.Navigate().GoToUrl("http://www.yahoo.com/");
    }
  }

and where you call the method in Class1:

    obj.method(driver);

Solution 4

You need to pass the reference of driver in Class1 to Class2 and assign it to the driver in there. When you pass by reference, you pass the memory address so the driver in Class2 becomes the same driver in Class1 because they both point to the same address in computer memory.

To pass the driver by reference in Class1 you need below;

obj.method(driver);

You need to modify Class2 so it can receive an IWebDriver in method().

Share:
27,318
jessica
Author by

jessica

Updated on May 06, 2020

Comments

  • jessica
    jessica almost 4 years

    When I run this program in NUnit I get an error

    Object reference not set to an instance of an object.

    Though this is not the original program I get a similar error there too. Any help appreciated. Exception occurs at

    driver.Navigate().GoToUrl("http://www.yahoo.com/");
    

    Program:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using NUnit.Framework;
    using OpenQA.Selenium.Firefox;
    using OpenQA.Selenium;
    
    namespace Class_and_object
    {
      [TestFixture]
      public class Class1
      {
         IWebDriver driver = null;
         [Test]
         public void test1()
         {
            class2 obj = new class2();
            driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("http://www.google.com/");
            obj.method();
         }
       }
      public class class2
      {
        IWebDriver driver = null;
        public void method()
        {
            driver.Navigate().GoToUrl("http://www.yahoo.com/");
        }
      }
    }
    
  • jessica
    jessica almost 11 years
    Thanks for the suggestion. Yes I get a NullReferenceException. So what should be written instead of Iwebdriver driver = null;
  • jessica
    jessica almost 11 years
    Yes I get a NullReferenceException. So what should be written instead of Iwebdriver driver = null;
  • Jon Skeet
    Jon Skeet almost 11 years
    @jessica: Well we don't know which object you want to call the method on. The same one that you instantiated in test1, or a different one? If you're fairly new to C# (which sounds like it's the case, if you were surprised by getting an exception here) I would strongly suggest that automating web tests isn't a great starting point - I suggest learning the core of the language more first.
  • Reza Shirazian
    Reza Shirazian almost 11 years
    You should instantiate your driver object before calling its navigate method.
  • It'sNotALie.
    It'sNotALie. almost 11 years
    You can't make a new instance of an interface... you need to instantiate a class that implements the interface.
  • nawfal
    nawfal almost 11 years
    @newStackExchangeInstance I dont think the answerer was providing actual code, but guiding OP what to do. None knows what the concrete type is..
  • jessica
    jessica almost 11 years
    Thank you. I appreciate your help.
  • Reza Shirazian
    Reza Shirazian almost 11 years
    You're right, I realized IWebDriver is an interface. Fixed it
  • It'sNotALie.
    It'sNotALie. almost 11 years
    Supposedly it's a FirefoxDriver.
  • jessica
    jessica almost 11 years
    I wanted to call it on test1 only. Yes you are right. Im a beginner trying to learn C#. So not sure how to correct the errors. Thanks for the suggestion and your help.
  • jessica
    jessica almost 11 years
    @reza Thanks alot. My program works now. I wanted to know how to call methods by creating an object. I know it dint work because i have set it as null but i didnt know an alternative way to write it. This is what I wanted. Thanks again.