-1

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();
kripindas
  • 480
  • 2
  • 7
  • 21
Ab123
  • 415
  • 2
  • 13
  • 34
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Ivar Apr 08 '18 at 10:41

1 Answers1

1

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";
Waqas Ahmed
  • 4,801
  • 3
  • 36
  • 45
  • properties = new Properties(); Now i have Initialized like this but still throwing null pointer exception in ConfigFileReader() Constructor. – Ab123 Apr 08 '18 at 10:59
  • You are not passing the correct path for configuration file. First get the value from System property then concate the remainging part . private final String propertyFilePath = System.getProperty("user.dir") + "//src//main//resources//configs/Configurations.properties"; – Waqas Ahmed Apr 08 '18 at 11:04
  • System.getProperty("user.dir") will return you what ? you are adding the whole path [//src//main//resources//configs/Configurations.properties] as key param to System.getProperty() – Waqas Ahmed Apr 08 '18 at 11:09
  • previous issue was with your property File initialization. Now you are not passing the correct path. Try to give hard code path and then see the results. Once fixed then give the correct path by reading the System property . – Waqas Ahmed Apr 08 '18 at 11:12
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/168494/discussion-between-waqas-ahmed-and-ab123). – Waqas Ahmed Apr 08 '18 at 11:13