Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我得到了一个“答案”;空指针异常";调用方法时发生异常。在selenium中使用PageFactory时_Java_Eclipse_Selenium Webdriver_Junit_Nullpointerexception - Fatal编程技术网

Java 我得到了一个“答案”;空指针异常";调用方法时发生异常。在selenium中使用PageFactory时

Java 我得到了一个“答案”;空指针异常";调用方法时发生异常。在selenium中使用PageFactory时,java,eclipse,selenium-webdriver,junit,nullpointerexception,Java,Eclipse,Selenium Webdriver,Junit,Nullpointerexception,我在调用方法时遇到com.sun.jdi.InvocationException出现“空指针异常”。在selenium中使用PageFactory时 我正在尝试使用Junit创建一个测试用例。当我试图调用父类中存在的方法时,我面临空指针异常 我的代码如下 class 1: public class Browser { public static RemoteWebDriver driver; public void openInternetExplorer(){ System.se

我在调用方法时遇到com.sun.jdi.InvocationException出现“空指针异常”。在selenium中使用PageFactory时

我正在尝试使用Junit创建一个测试用例。当我试图调用父类中存在的方法时,我面临空指针异常

我的代码如下

class 1:

public class Browser {

public static RemoteWebDriver driver;

public void openInternetExplorer(){
    System.setProperty("webdriver.ie.driver","path to ie driver");
      driver = new InterExplorerDriver();
 driver.get("https://www.gmail.com")
    }
}

Class2:

public class Elements extends Browser{

public enterText(WebElement element, String text){

element.sendKeys(text);
}
}

Class3:

public BaseClass extends Elements{

BaseClass(){
PageFactory.initElements(driver,this);
}

}

Class4:

public Testing extends BaseClass{

@FindBy(id="unique_name") WebElement uniqName;


public void method1(){
openInternetExplorer();
enterText(uniqName,"SuperMan");

}

}


Error Which i am getting is

    java.lang.NullPointerException
    at        org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(Defaul  tElementLocator.java:69)
    at      org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(L     ocatingElementHandler.java:38)
    at com.sun.proxy.$Proxy7.click(Unknown Source)
    at com.framework.Browser.clickAction(Element.java:5)
    at com.framework.Testing.Testing(Testing.java:7)

如果我错过了什么,请告诉我

必须执行
PageFactory.initElements(驱动程序,this)
在调用
enterText(uniqName,“SuperMan”)之前的测试类中方法来调用元素


否则您的@FindBy元素将不会被调用。

感谢您的快速回复。我已经添加了PageFactory.initElements(driver,this);在基类构造函数和扩展类中。我猜构造函数将首先被调用,然后它将继续执行@FindBy(),如果@FindBy()是,那么它将只调用基类中定义的元素,这些元素不适用于测试类WebElementsThank you kushal。当我添加PageFactory.initElements(驱动程序,this)时,它工作了;特别是在enterText()之前,类似于这个PageFactory.initElements(驱动程序,这个);enterText();然后它成功了。