Tokci Tokci - 20 days ago 7
Java Question

Why does adding these 3 lines prevent a NullPointerException?

If I am using

Test_Will_Give_Null_Pointer_Error
method I get a
NullPointerException


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


which is line
driver.get(prop.getProperty("URL"));

and debugging shows
prop
is
null
.

If I add below lines to
openURL()
code works fine.

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


Error code

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
}
}


Below code works fine.

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


Questions:


  1. I declared objects driver, prop, fls at class level so they are not local, once I instantiate them in
    openBrowser
    method, their values should be carried over to
    openURL()
    method. Using this logic I work with driver object and don't see any
    NullPointerException
    .

  2. If above concept is wrong, then why does the driver object not throw a
    NullPointerException
    ?


Answer

You override the prop global field:

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

To assign the new properties to the global prop field you need to do:

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

Note that this will only work if you call openBrowser() first, as the prop field will be otherwise not initialized.

You normally should not create a local field with the same name as a global field, as this produces these kind of errors quite easily.

To be sure you initialize the fields only once (and they are initialized when you want to use them) make them final and assign them in the constructor:

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