Ujjwal Kumar
Ujjwal Kumar

Reputation: 1

Facing NullPointerException in Selenium

I have created three files: one is index file, other is configuration file and last the last one is the property file. While executing the code I'm getting NULLPointerException.

I am not able to solve this issue. Please help me to rectify this code.

index.java:

package main;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import config.Configuration;

public class Index 
{
    WebDriver driver;
    @Test(priority = 1)
    public void handling_multiple_windows() throws Exception
    {
        Configuration obj = new Configuration();
        System.setProperty("webdriver.chrome.driver", obj.path());
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get(obj.handling_window_url());
    }
}

Configuration.java:

package config;
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import org.openqa.selenium.WebDriver;

public class Configuration 
{
    Properties pro;
    WebDriver driver;
    public Configuration() throws Exception
    {
        File f = new File("./Config/config.property");
        FileInputStream fis = new FileInputStream(f);
        Properties pro = new Properties();
        pro.load(fis);  
    }   

    public String path()
    {
        String url = pro.getProperty("ChromeDriverPath");
        return url;
    }

    public String handling_window_url()
    {
        return pro.getProperty("URL");
    }
}

config.property:

ChromeDriverPath = G:\\Selenium Webdriver\\chromedriver\\chromedriver.exe
URL = https://www.naukri.com

Upvotes: 0

Views: 76

Answers (2)

iamsankalp89
iamsankalp89

Reputation: 4739

Remove Properties from Properties pro = new Properties(); from this code and make it:

pro = new Properties();

You already declare it above, so need to declare it again

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193058

The reason you get a NullPointerException is because in the Configuration.java Class you have declared Properties pro; globally but again within the Configuration() constructor you have again initiated another instance of Properties as Properties pro = new Properties();. Hence the NullPointerException.

Change the line:

Properties pro = new Properties();

to:

pro = new Properties();

Your code will work fine.

Upvotes: 1

Related Questions