C# 你调用的对象是空的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16862977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 07:58:57  来源:igfitidea点击:

Object reference not set to an instance of an object

c#seleniumnullselenium-webdrivernullreferenceexception

提问by jessica

When I run this program in NUnit I get an error

当我在 NUnit 中运行这个程序时出现错误

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/");
    }
  }
}

采纳答案by Reza Shirazian

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();
 }

回答by Jon Skeet

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- driveris always null.

当然,您得到的是NullReferenceException-driver总是null

It's not clear what you expected to happen here - but perhaps you meant to pass the FirefoxDriveryou instantiate in test1into methodvia a parameter?

目前尚不清楚您希望在这里发生什么 - 但也许您打算通过参数将FirefoxDriver实例化test1传入method

回答by It'sNotALie.

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

你分配driver你的Class1,所以当它试图在导航class2method失败,为class2drivernull。在调用任何方法之前,您需要为其分配一个值。

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

我不知道为什么你希望它以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:

以及您在其中调用方法的位置Class1

    obj.method(driver);

回答by Alex

You need to pass the reference of driverin Class1to Class2and assign it to the driverin there. When you pass by reference, you pass the memory address so the driverin Class2becomes the same driverin Class1because they both point to the same address in computer memory.

您需要将driverin的引用传递Class1Class2并将其分配给driverin there。当您通过引用传递时,您传递的是内存地址,因此driverinClass2变为相同的driverin,Class1因为它们都指向计算机内存中的相同地址。

To pass the driver by reference in Class1you need below;

要通过引用传递驱动程序,Class1您需要在下面;

obj.method(driver);

You need to modify Class2so it can receive an IWebDriverin method().

您需要进行修改,Class2以便它可以接收IWebDriverin method()