Search code examples
javacucumber-java

Getting Null pointer exception while loading property file in selenium java


This is the java code for file reader

package dataProviders;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class ConfigFileReader {

private Properties properties;
private final String propertyFilePath = System.getProperty("user.dir"+"//src//main//resources//configs/Configurations.properties");

public ConfigFileReader() throws IOException
{

        FileInputStream fis = new FileInputStream(propertyFilePath);
        properties.load(fis);
        fis.close();

}

public String getUrl()
{
    String url = properties.getProperty("url");

    if(url!=null)
    {

        return url;

    }

    else
    {
        throw new RuntimeException("URL is not specified in configuration.properties file");
    }
}

public String driverpath()
{

    String driverpath = properties.getProperty("driverpath");

    if(driverpath!=null)
    {

        return driverpath;
    }
    else
    {
        throw new RuntimeException("Driver path is not specified in configuration.properties"); 
    }

}

}

This is the java code which contains cucumber Annotation and which works as main method

package stepDefinations;

import java.util.List;
import java.util.concurrent.TimeUnit;

import junit.framework.Assert;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import pageObjects.CartPage;
import pageObjects.Checkoutpage;
import pageObjects.HomePage;
import pageObjects.ProductListingPage;
import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.When;
import dataProviders.ConfigFileReader;

public class EndtoEndTest {

WebDriver driver;
ConfigFileReader cnffilered;


@Given("^User is on Homepage$")
public void user_is_on_Homepage() throws Throwable {


    cnffilered = new ConfigFileReader();
    System.setProperty("webdriver.chrome.driver",cnffilered.driverpath());
    driver = new ChromeDriver();
    driver.get(cnffilered.getUrl());
    driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
    driver.manage().window().maximize();




}

@When("^he searches for \"([^\"]*)\"$")
public void he_searches_for(String arg1) throws Throwable {


    HomePage home = new HomePage(driver);
    home.perform_Search(arg1);
}

@When("^Choose to buy the first item$")
public void choose_to_buy_the_first_item() throws Throwable {
    ProductListingPage productListingPage = new ProductListingPage(driver);
    productListingPage.select_Product(0);
    productListingPage.clickOn_AddToCart(); 
}

@When("^moves to checkout from mini cart$")
public void moves_to_checkout_from_mini_cart() throws Throwable {
    CartPage cartPage = new CartPage(driver);
    cartPage.clickOn_Cart();
    cartPage.clickOn_ContinueToCheckout();
}

@When("^enter personal details onn checkout page$")
public void enter_personal_details_onn_checkout_page() throws Throwable {


    Checkoutpage checkoutPage = new Checkoutpage(driver);
    checkoutPage.fill_PersonalDetails();    
}

@When("^select same delivery address$")
public void select_same_delivery_address() throws Throwable {
    Checkoutpage checkoutPage = new Checkoutpage(driver);
    checkoutPage.check_ShipToDifferentAddress(false);
}

@When("^select payment method as \"([^\"]*)\" payment$")
public void select_payment_method_as_payment(String arg1) throws Throwable {
    Checkoutpage checkoutPage = new Checkoutpage(driver);
    checkoutPage.select_PaymentMethod("CheckPayment");
}

@When("^place the order$")
public void place_the_order() throws Throwable {
    Checkoutpage checkoutPage = new Checkoutpage(driver);
    checkoutPage.check_TermsAndCondition(true);
    checkoutPage.clickOn_PlaceOrder();

    driver.quit();
}
}

While Executing my Cucumber feature file i am getting below exception and i tried many things but still don't know why it is throwing Null Pointer Exception

java.lang.NullPointerException
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at dataProviders.ConfigFileReader.<init>(ConfigFileReader.java:17)
at stepDefinations.EndtoEndTest.user_is_on_Homepage(EndtoEndTest.java:32)
at ✽.Given User is on Homepage(src/test/resources/functionalTest/EndtoEndTest.feature:8)

Any Help will be appreciated. Thanks in advance.

ConfigFileReader.Java17: FileInputStream fis = new FileInputStream(propertyFilePath);

EndtoEndTest.java32:cnffilered = new ConfigFileReader();

Solution

  • where your attribute properties is initialized in class ConfigFileReader?

    private Properties properties;

    After reading your code it seems that property object will always be null. You are not injecting it from outside, neither initializing it.

    Please try Properties prop = new Properties(); Plus your are not passing the correct value for propertyFilePath variable. wrong concatenation . Correct one will be as below

    private final String propertyFilePath = System.getProperty("user.dir") + "//src//main//resources//configs/Configurations.properties";