2

I'm a beginner in C#. I get a Null Reference Exception when I'm instantiating a class in NUnit programming. I have two separate class- a global library and the main program. I want to use the instantiated class(globalLib) both in setup and test. The exception error is because I have set the globalLib as null. How do I correct this?. How should the class be instantiated so that it can be used in setup and test? I have given the code below. Any help appreciated. Thanks.

My code:

Class 1:

[TestFixture]
public class LandForSale
{
    private IWebDriver driver = null;
    GlobalLibrary globalLib = null;
    [SetUp]
    public void OpenBrowser()
    {
        globalLib = new GlobalLibrary(driver);
        globalLib.StartDriver();           
    }
    [Test]
    public void TestScenario()
    {
        string[] setofitems = { "Residential", "Commercial" };
        foreach (string item in setofitems)
        {
           globalLib.OpenUrl();
           globalLib.Search();
           etc...
        }
    }       
}

}

Class 2:

public class GlobalLibrary
{
    IWebDriver driver = null;
    public GlobalLibrary(IWebDriver driver)
    {
        this.driver = driver;
    }
    public IWebDriver StartDriver()
    {
        driver = new FirefoxDriver();
        return driver;
    }
    public void OpenUrl()
    {
        driver.Navigate().GoToUrl("http://www.auction.com/");
        driver.Manage().Window.Maximize();
    }
    public void Search()...
}

}

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
jessica
  • 1,507
  • 3
  • 15
  • 18
  • 3
    Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Aug 21 '13 at 19:21
  • 1
    Your class names don't match. You try to instantiate LocalLibrary, but I don't see that class in your code. – Nick Zimmerman Aug 21 '13 at 19:24
  • @NickZimmerman... Typing error, the locallibrary was supposed to be globallibrary. – jessica Aug 21 '13 at 19:28
  • 1
    if your class GlobalLibrary has an internal driver that is beeing initialized in the StartDriver method why do you that as a variable in the LandForSale since you never use it ? besides StartDriver returns the driver but that value returned is never assigned – Mauricio Gracia Gutierrez Aug 21 '13 at 19:29
  • 1
    Set a breakpoint on the "driver = new FirefoxDriver()" line to see what is that method returning and make sure that the driver is not null at that point – Mauricio Gracia Gutierrez Aug 21 '13 at 19:41
  • @MauricioGracia Will check that. – jessica Aug 21 '13 at 20:02

2 Answers2

1

NUnit will run the [SetUp] tagged method before each method tagged [Test]. And in your setup method, you are passing a null value into the constructor. You should be passing in a driver at that point:

globalLib = new GlobalLibrary(new SomeDriverClass());

Also, I think your GlobalLibrary class would benefit from a better constructor:

public class GlobalLibrary
{
    private IWebDriver driver = null;
    //Constructor with optional parameter allows you to pass in driver of choice
    // OR pass in nothing and get the default Firefox driver 
    public GlobalLibrary(IWebDriver initDriver = null)
    {
        this.driver = initDriver ?? new FirefoxDriver();
    }
    //StartDriver is no longer necessary, but we might want to be able to
    // grab the driver that is being used, so we'll add this read-only property.
    public IWebDriver CurrentDriver  
    {
        get
        {
            return driver;
        }
    }

    //Rest of methods
}
User
  • 1,118
  • 7
  • 23
  • @peter... Thanks this method works fine.But i have an assignment and im supposed to open the browser from setup method by using the globalLib.StartDriver() and it shows null error. Can you tell me how to correct this ? – jessica Aug 21 '13 at 19:56
  • Is the exception getting thrown inside the StartDriver method, or when you go to call it? – User Aug 21 '13 at 20:07
  • No the exception is thrown in OpenUrl(). It passes through StartDriver() and a new window is opened. It fails to load auction.com in OpenUrl(). – jessica Aug 21 '13 at 20:17
  • If your code successfully steps over the the line `driver = new FirefoxDriver();` without incident, then it sounds like your problem is with the actual driver. In that case, I recommend opening a new question on how to properly use an IWebDriver. – User Aug 21 '13 at 20:25
  • Ok will do that. Thanks. – jessica Aug 21 '13 at 20:26
0

I added a comment at the problem area

[TestFixture]
public class LandForSale
{
    private IWebDriver driver = null;
    GlobalLibrary globalLib = null;
    [SetUp]
    public void OpenBrowser()
    {
        globalLib = new GlobalLibrary(driver);
        globalLib.StartDriver();           
    }
    [Test]
    public void TestScenario()
    {
        string[] setofitems = { "Residential", "Commercial" };
        foreach (string item in setofitems)
        {
           globalLib.OpenUrl(); //If the OpenBrowser() method is not called then globalLib is null
           globalLib.Search();
           etc...
        }
    }       
}

I would suggest adding a constructor that instantiates your driver and globalLib fields

BrianM
  • 173
  • 3
  • 14
  • @BrianM.. Yes the openbrowser method is not called because globalLib is null. So what should i assign to globalLib instead of null? – jessica Aug 21 '13 at 19:36
  • 1
    In NUnit, [SetUp] is called before each [Test] – User Aug 21 '13 at 19:36
  • @Jessica you cannot call a method on a null object. So you would need to makes sure you instantiate the object before you make method calls. – BrianM Aug 21 '13 at 19:40
  • Yes i have instantiated the class and then called it in StartDriver(). Cause i have to use globalLib in setup as well as test i assigned globalLib in testfixture as null. But if i instantiate the class in test only my program works fine. Even here the browser opens but it fails in OpenBrowser() – jessica Aug 21 '13 at 19:45
  • @jessica I see now. I missed the `[SetUP]` attribute. In the . Can you tell at which line is killing the execution? – BrianM Aug 21 '13 at 19:56
  • A new firefox window opens but it does not go to the url ... It stops in Openurl() – jessica Aug 21 '13 at 20:00
  • @jessica it sound like your issue is is in `.Navigate()`. Is `FirefoxDriver` from a third party reference? If not, could you share some of that code, specifically `.Navigate()` – BrianM Aug 21 '13 at 20:06
  • No it is not. i dont have anything else in the OpenUrl(). It just navigates to auction.com and maximizes the page. It then goes to the next method Search() where it types commercial and searches for it. – jessica Aug 21 '13 at 20:14
  • Ok Since Im new I have trouble in finding where exactly the error occurs. So what should be done to avoid the null reference coming from firefoxdriver ? – jessica Aug 21 '13 at 20:23
  • Do you know how to use the debugger? Step through you code line by line and check you local variables and fields to see where the null is. – BrianM Aug 21 '13 at 20:24
  • @BrianM.. Gimme me a minute will check it – jessica Aug 21 '13 at 20:34
  • @BrianM.. Im a student.. I dont have the ultimate version. I have visual studio 2010 express edition which does not have debug option for nunit programs. I tried using external debugger but they dont seem to work. – jessica Aug 21 '13 at 20:47
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35948/discussion-between-brianm-and-jessica) – BrianM Aug 21 '13 at 20:57