Skip to content

Java cucumber Selenium : NullPointerException for scenario.write for the first scenario on before tag

I am getting the NullPointerException when the code below code is executed. Note that the exception is recieved only for the first cucumber scenario executed. There is no exception for the second scenario. I am actually trying to insert the timestamp before start of each scenario. Please advise how this issue can be fixed. Any help will be appreciated.

@Before //cucumber.api.java
public void beforeScenario(Scenario scenario1)
{
this.scenario=scenario1; //I would need this for a later use.
scenario1.write("Start Time:"+new Date()); //Nullpointerexception

}

I tried this as well, but the same error.

@Before //cucumber.api.java
public void beforeScenario(Scenario scenario)
{
this.scenario=scenario; //wont matter if i have this or not
scenario1.write("Start Time:"+new Date()); //Nullpointerexception

}

Answer

Was unable to find a direct answer. However As an alternative : i avoided writing on “scenario” in @Before

@Before //cucumber.api.java
public void beforeScenario(Scenario scenario)
{
startDate = new Date(); //defined globally
}

@After//cucumber.api.java
public void afterScenario(Scenario scenario)
{
this.scenario=scenario; 
scenario1.write("StartTime:"+startDate);
scenario1.write("End Time:"+new Date());
}