为什么添加这 3 行可以防止 NullPointerException?
Why does adding these 3 lines prevent a NullPointerException?
如果我使用 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"));
行
调试显示 prop
是 null
.
如果我将以下行添加到 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"));
}
}
问题:
- 我在 class 级别声明了对象 driver、prop、fls,所以它们不是本地的,一旦我在
openBrowser
方法中实例化它们,它们的值应该传递给 openURL()
方法。使用此逻辑我使用驱动程序对象并且看不到任何 NullPointerException
.
- 如果上面的概念是错误的,那么驱动对象为什么不抛出一个
NullPointerException
?
您覆盖了 prop 全局字段:
public void openBrowser() throws IOException
{
Properties prop = new Properties(); // HERE, this is a local field
}
要将新属性分配给全局道具字段,您需要执行以下操作:
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\chromedriver.exe");
driver = new ChromeDriver(); // Sets the global field
}
public void openURL()
{
driver // Accesses the global field
.get(prop // Accesses the global field
.getProperty("URL"));
// ...
}
如果我使用 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"));
行
调试显示 prop
是 null
.
如果我将以下行添加到 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"));
}
}
问题:
- 我在 class 级别声明了对象 driver、prop、fls,所以它们不是本地的,一旦我在
openBrowser
方法中实例化它们,它们的值应该传递给openURL()
方法。使用此逻辑我使用驱动程序对象并且看不到任何NullPointerException
. - 如果上面的概念是错误的,那么驱动对象为什么不抛出一个
NullPointerException
?
您覆盖了 prop 全局字段:
public void openBrowser() throws IOException
{
Properties prop = new Properties(); // HERE, this is a local field
}
要将新属性分配给全局道具字段,您需要执行以下操作:
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\chromedriver.exe");
driver = new ChromeDriver(); // Sets the global field
}
public void openURL()
{
driver // Accesses the global field
.get(prop // Accesses the global field
.getProperty("URL"));
// ...
}