gpt4 book ai didi

java - 为什么添加这 3 行可以防止 NullPointerException?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:37:14 25 4
gpt4 key购买 nike

如果我使用 Test_Will_Give_Null_Pointer_Error 方法,我会得到一个 NullPointerException

Stack trace
FAILED: openURL
java.lang.NullPointerException
at SeleniumPracticePackage.CallUrl.**openURL**(CallUrl.java:63)

这是行 driver.get(prop.getProperty("URL"));调试显示 propnull

如果我在 openURL() 中添加以下行,代码就可以正常工作。

Properties prop = new Properties();
FileInputStream fis = new FileInputStream("C:\\Users\\XXXX\\src\\URL.properties");
prop.load(fis);

错误代码

 public class Test_Will_Give_Null_Pointer_Error {       
WebDriver driver;
Properties prop ;
FileInputStream fis;
@BeforeTest
public void openBrowser() throws IOException
{
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("C:\\Users\\XXXX\\src\\URL.properties");
prop.load(fis);
String browserType = prop.getProperty("Browser");

//ignored Chromedriver code below
}
@Test
public void openURL() throws IOException
{
driver.get(prop.getProperty("URL"));
//ignored rest of code
}
}

下面的代码工作正常。

public class TestRunsFine {     
WebDriver driver;
Properties prop ;
FileInputStream fis;
@BeforeTest
public void openBrowser() throws IOException
{
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("C:\\Users\\XXXX\\src\\URL.properties"); prop.load(fis);System.setProperty("webdriver.chrome.driver","C:\\xxxxx\\chromedriver.exe");
WebDriver driver = new ChromeDriver();

}
@Test
public void openURL() throws IOException
{
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("C:\\Users\\xxxx\\URL.properties");
prop.load(fis);
driver.get(prop.getProperty("URL"));
}
}

问题:

  1. 我在类级别声明了对象 driver、prop、fls,因此它们不是本地的,一旦我在 openBrowser 方法中实例化它们,它们的值应该被传递到 openURL()方法。使用此逻辑,我使用驱动程序对象并且没有看到任何 NullPointerException
  2. 如果上述概念是错误的,那么驱动对象为什么不抛出一个NullPointerException

最佳答案

你覆盖了 prop 全局字段:

public void openBrowser() throws IOException
{
Properties prop = new Properties(); // HERE, this is a local field
}

要将新属性分配给全局 prop 字段,您需要执行以下操作:

public void openBrowser() throws IOException
{
prop = new Properties(); // Assign to global field
}

请注意,这仅在您首先调用 openBrowser() 时有效,否则 prop 字段将不会初始化。

您通常不应创建与全局字段同名的本地字段,因为这很容易产生此类错误。

为了确保你只初始化字段一次(并且当你想使用它们时它们被初始化)使它们成为最终的并在构造函数中分配它们:

private final Properties prob; // Global field
private final WebDriver driver; // Global field

public Constructor_for_your_class()
{
prop = new Properties(); // Sets the global field
FileInputStream fis = new FileInputStream("C:\\Users\\XXXX\\src\\URL.properties");
prop.load(fis);
System.setProperty("webdriver.chrome.driver",‌​"C:\\xxxxx\\chromedr‌​iver.exe");
driver = new ChromeDriver(); // Sets the global field
}

public void openURL()
{
driver // Accesses the global field
.get(prop // Accesses the global field
.getProperty("URL"));
// ...
}

关于java - 为什么添加这 3 行可以防止 NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39821820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com