为什么我得到空指针异常 [英] Why am I getting null pointer exception

查看:118
本文介绍了为什么我得到空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class IbiboTest {

static WebDriver driver;

@BeforeClass
public void setUp() throws InterruptedException{
    System.setProperty("webdriver.chrome.driver","C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();

    //driver= new FirefoxDriver();
    driver.get("https://www.goibibo.com/");
    Thread.sleep(5000);
    driver.manage().window().maximize();

}


@Test
public void testIbiboHomePage(){
    IbiboHomePage home = PageFactory.initElements(driver, com.Nalini.Ibibo.IbiboHomePage.class);
    home.clickRoundTripRadioButton();

}

public class IbiboHomePage {

WebDriver driver;
@FindBy(css = "input[id='gi_roundtrip_label']")
WebElement iRoundTrip;


public IbiboHomePage(WebDriver driver){
    this.driver = driver;
    PageFactory.initElements(driver, this);

}

public void clickRoundTripRadioButton(){
iRoundTrip.click();
}





我的尝试:



我只是想尝试自动化ibibo网站。我正在获取上述代码的nullpointer异常。我无法理解它传递空值的位置。请帮忙。谢谢你



What I have tried:

I am just trying to automate ibibo website.I am getting nullpointer exception for the above code.I am not able to understand where it is passing a null value.Pls help.Thank you

推荐答案

您正在声明一个静态 IbiboTest 类成员:

You are declaring a static IbiboTest class member:
static WebDriver driver;

setUp()函数中,您正在声明另一个本地实例:

In your setUp() function you are declaring another local instance:

WebDriver driver = new ChromeDriver();

因此,静态类成员仍然未初始化,并且在 testIbiboHomePage()函数中出现空指针异常。



解决方案:

setUp():

As a result, the static class member is still uninitialised and you get a null pointer exception in your testIbiboHomePage() function.

Solution:
Remove the type when creating the driver in setUp():

driver = new ChromeDriver();


这篇关于为什么我得到空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆