title: TestNg+ExtentReport: Null Pointer exception while executing 2 classes through testng.xml --when I use Extent report in @BeforeSuite
body: <p>ExtentReports report;<br>
  ExtentTest logger; <br>
  Code runs correct for 1 class but throws null pointer exception for 2nd class when I use Extent report <br>
  1. I initialized  in @BeforeSuite<br>
  2. Then Initialized  in @BeforeMethod <br>
  3. In testng.xml there are 2 classes Class1 &amp; Class2 <br>
  4. On execution of testng.xml - All @Test of class1 runs completely but Class2 throws null pointer exception error when it reads   in BeforeMethod(Initialized as mentioned in step2)<br></p>
  
  <ol start="5">
  <li>Extent report has been initialized in Testbase class and created <b>getter</b> for it so that other class can read it <br>
  Note: When I change BeforeSuite to BeforeClass i.e initialization of  is done in BeforeClass then It runs fine <b>but it produces extent report of only class1. </b></li>
  </ol>
  
  <p>Also I am using Aftermethod to Flush the report and quit driver. Any solution to get rid of this null pointer exception</p>
  
  <p>Below is the complete code <br>
      <b>1. TestBase Class</b>  </p>
  
  <pre><code>package sampletestproject;
  
  import java.io.File;
  import java.io.IOException;
  import java.text.DateFormat;
  import java.util.Date;
  import java.util.concurrent.TimeUnit;
  import org.apache.commons.io.FileUtils;
  import org.openqa.selenium.OutputType;
  import org.openqa.selenium.TakesScreenshot;
  import org.openqa.selenium.WebDriver;
  import 
   org.openqa.selenium.chrome.ChromeDriver;
  import 
  org.openqa.selenium.firefox.FirefoxDriver;
  import 
  org.openqa.selenium.support.PageFactory;
  import org.testng.ITestContext;
  import org.testng.ITestNGMethod;
  import org.testng.annotations.AfterClass;
  import org.testng.annotations.BeforeSuite;
  
  
  
  public class TestBasee {
      private ExtentReports report;   
      public WebDriver driverObj;
      public Homepagee homeObj;
  
  @BeforeSuite (alwaysRun = true)
  public void beforeTest(){
      System.out.println("In @BeforeSuite");
      report = new ExtentReports("G:\\ExtentReport"+fn_GetTimeStamp()+".html");
      System.out.println("Out @BeforeSuite");
  }
  
  
  
  @AfterClass(alwaysRun = true)
  public void tearDown(ITestContext context) throws IOException, InterruptedException{
      System.out.println("@AfterClass In tear down");
      ITestNGMethod[] tngMethods  = context.getAllTestMethods();
      int i=1;
      for(ITestNGMethod tng: tngMethods){         
          String methodName = "Method"+i+": "+ tng.getMethodName();
          i++;
          System.out.println(methodName);
      }
  }
  public static String fn_GetTimeStamp() {
      DateFormat DF = DateFormat.getDateTimeInstance();
      String DateVal = DF.format(new Date());
      DateVal = DateVal.replaceAll("/", "_");
      DateVal = DateVal.replaceAll(",", "_");
      DateVal = DateVal.replaceAll(":", "_");
      DateVal = DateVal.replaceAll(" ", "_");
      return DateVal;
  }
                  /******************** Open Site **************************/
  public Homepagee gm_OpenApp(String BrowserName, String URL) throws Exception {
      System.out.println("In gm_OpenAp Method");
      System.out.println(BrowserName+" -- "+URL);     
      gm_LaunchBrowser(BrowserName);
      Thread.sleep(2000);
      gm_OpenURL(URL);
      Thread.sleep(2000);
      homeObj = PageFactory.initElements(driverObj, Homepagee.class);
      return homeObj;
  }
  public void gm_OpenURL(String URL) {
      driverObj.get(URL);
  }   
  public void gm_LaunchBrowser(String browserName) throws Exception{
      // Launch Chrome browser
       if (browserName.equalsIgnoreCase("CH") == true) {
           System.setProperty("webdriver.chrome.driver", "MasterFiles\\Drivers\\ChromeDriver\\Chromedriver_win32_v2.38\\chromedriver.exe");
          driverObj = new ChromeDriver();
       }
       // Launch Firefox browser
       else if (browserName.equalsIgnoreCase("FF") == true) {
           System.setProperty("webdriver.gecko.driver", "MasterFiles\\Drivers\\GeckoDriver\\64Bit\\v20\\geckodriver.exe");
           driverObj = new FirefoxDriver();
       }
       driverObj.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
       driverObj.manage().timeouts().pageLoadTimeout(250, TimeUnit.SECONDS);
       driverObj.manage().window().maximize();
  
  }
                              /****************TAKE SCREENSHOT*****************/
  public String gm_setScreenshotPath_forExtentReporting(String elementName, String resultStatus) {
      System.out.println("In gm_setScreenshotPath_forExtentReporting");
      System.out.println(elementName + "--" + resultStatus);
      String screenshotPath = "G:\\QA\\AutomationTools\\Selenium\\WorkspaceMars1\\1.2hp.com.automationprac\\TestReports\\ExtentReport\\Screenshots\\"+ resultStatus + "\\" + elementName + "_" + fn_GetTimeStamp() + ".png";
      return screenshotPath;
  }
  public void gm_TakeSnapshot(String destFilePath) throws IOException, InterruptedException {
      TakesScreenshot tss = (TakesScreenshot) driverObj;
      File srcfileobj = tss.getScreenshotAs(OutputType.FILE);
      File destFileObj = new File(destFilePath);
      FileUtils.copyFile(srcfileobj, destFileObj);
  
  }
  public ExtentReports getReport() {
      return report;
  }
  
  
  public void setReport(ExtentReports report) {
      this.report = report;
    }
  
    }
  </code></pre>
  
  <p><b>2. SignIn -Page Object</b></p>
  
  <pre><code>package sampletestproject;
  
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.WebElement;
  import org.openqa.selenium.support.FindBy;
  
  public class SignInPagee extends TestBasee{
  @FindBy(xpath = "//div[@id = 'center_column']/h1")
   public static WebElement PageHeading_SignIn_txt;
  
   @FindBy(xpath = "//h3[contains(text(), 'Already registered?')]")
   public static WebElement SectionHeading_SignIn_txt;
  
  
      public SignInPagee(WebDriver driverObj){
          this.driverObj = driverObj;      
      }
  
    }
  </code></pre>
  
  <p><b>3. Homepage - PageObject </b><br></p>
  
  <pre><code>package sampletestproject;
  import java.io.IOException;
  import java.util.List;
  import java.util.NoSuchElementException;
  import java.util.concurrent.TimeUnit; 
  import org.openqa.selenium.By;
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.WebElement;
  import 
  org.openqa.selenium.chrome.ChromeDriver;
  import org.openqa.selenium.support.FindBy;
  import 
  org.openqa.selenium.support.PageFactory;
  import 
  org.openqa.selenium.support.ui.FluentWait;
  import org.openqa.selenium.support.ui.Wait;
  import 
  import com.google.common.base.Function;
  
  public class Homepagee extends TestBasee {
  
  
  public Homepagee(WebDriver driverObj){
        this.driverObj = driverObj;
    }
  
   public SignInPagee navigateToSignInPage(){
       System.out.println("In navigateToSignInPage"); 
       driverObj.navigate().to("http://automationpractice.com/index.php?controller=authentication&amp;back=my-account");
       SignInPagee signInPageObj = PageFactory.initElements(driverObj, SignInPagee.class);
       return signInPageObj;
     }
  
  
  }
  </code></pre>
  
  <p><b>4. HomepageTest -Testpage</b> <br></p>
  
  <pre><code>package sampletestproject;
  import java.lang.reflect.Method;
  import org.testng.Assert;
  import org.testng.ITestResult;
  import org.testng.annotations.AfterMethod;
  import org.testng.annotations.BeforeMethod;
  import org.testng.annotations.Parameters;
  import org.testng.annotations.Test;
  import com.relevantcodes.extentreports.ExtentReports;
  import com.relevantcodes.extentreports.ExtentTest;
  import com.relevantcodes.extentreports.LogStatus;
  
  public class HomepageeTest extends TestBasee {
      //ExtentReports report;
      ExtentTest logger;
      String elementName;
      String Comment;
      String actualResult;
      String expectedResult;
  
  
  
      @BeforeMethod(alwaysRun = true)
      @Parameters({ "Browser", "URL"})
      public void getBrowser(Method method, String Browser, String URL) throws Exception{
          logger = getReport().startTest((this.getClass().getSimpleName()+"::"+method.getName()), method.getName());
          logger.assignAuthor("VK");
          logger.assignCategory("HomePage - Smoketesting and TextTesting--Live");
          homeObj = gm_OpenApp(Browser, URL);             
  
      }
       @AfterMethod (alwaysRun = true)
          public void publishReport_SIP(ITestResult result) throws Exception{
              System.out.println("publishReport_SIP");
              String resultStatus = null;         
              if(result.getStatus() == ITestResult.FAILURE){              
                  resultStatus = "FAILED"; 
                  String screenshot_Path = gm_setScreenshotPath_forExtentReporting(elementName, resultStatus);            
                  gm_TakeSnapshot(screenshot_Path);               
                  String image = logger.addScreenCapture(screenshot_Path);
                  logger.log(LogStatus.FAIL, Comment, image);
              }else{              
                  resultStatus = "PASSED";                
                  String screenshot_Path = gm_setScreenshotPath_forExtentReporting(elementName, resultStatus);
                  gm_TakeSnapshot(screenshot_Path);
                  System.out.println("screenshot_Path: "+screenshot_Path);
                  String image = logger.addScreenCapture(screenshot_Path);
                  logger.log(LogStatus.PASS, Comment, image);
              }
              getReport().endTest(logger);
              getReport().flush();
              System.out.println("closing now_SIP.");
              driverObj.quit();
          }       
  
  //"********Validation of SignIn Link********");
  @Test(priority=0, groups = {"Smoke"})   
  public  void validateHeaderSignInLink_HP() throws Exception{        
      System.out.println("In validateHeaderSignInLink Method_HP"); 
      elementName = "SignInLink";
      Comment = "validateHeaderSignInLink";
      actualResult = "http://automationpractice.com/index.php?controller=authentication&amp;back=my-account";
      expectedResult = "http://automationpractice.com/index.php?controller=authentication&amp;back=my-account";
      Assert.assertEquals(actualResult, expectedResult);
      System.out.println("Out  of validateHeaderSignInLink method_HP");
  }       
  
  //"********Validation of GetSavingNow Button********");
  @Test (priority = 1, groups = {"Smoke"})
  public  void validateGetSavingNowButton_HP() throws Exception{
      System.out.println("In validateGetSavingNowButton Method_HP");
      elementName = "GETSAVINGSNOWButton";        
      Comment = "validateGetSavingNowButton"; 
      expectedResult = "http://automationpractice.com/index.php";
      actualResult = "http://automationpractice.com/index.php";
      Assert.assertEquals(actualResult, expectedResult);      
      System.out.println("Out  of validateGetSavingNowButton method_HP");
  }   
  
  @Test (priority = 2, groups = {"UITest"})
  //"********Validation of SearchBox********");
  public  void validateSearchField_HP() throws Exception{
      System.out.println("In validateSearchField Method_HP");     
      elementName = "FadedShortSleeveTshirts_lnktxt";     
      Comment = "validateSearchField";        
      actualResult = "Faded Short Sleeve T-shirtss";  //Just to produce a failed result
      expectedResult = "Faded Short Sleeve T-shirts";
      Assert.assertEquals(actualResult, expectedResult);
      System.out.println("Out  of validateSearchField method_HP");
  }
  
  
   @Test (priority = 3, enabled = true, groups = {"Smoke", "UITest"})
  //"********Validation of Slider1********");  
  public  void validateHomepageSlider1_HP() throws Exception{
      System.out.println("In validateHomepageSlider1 Method_HP"); 
      elementName = "Homepage Slider1";
      Comment = "validateHomepageSlider1";
      actualResult =  "https://www.prestashop.com/en?utm_source=v16_homeslider";
      expectedResult = "https://www.prestashop.com/en?utm_source=v16_homeslider";
      Assert.assertEquals(actualResult, expectedResult);  
      System.out.println("Out  of validateHomepageSlider1 method_HP");
  
      }   
  
    }
  </code></pre>
  
  <p><b>5. SignIntest Class- Testpage </b> <br>
      package sampletestproject;</p>
  
  <pre><code>import java.lang.reflect.Method;
  
  import org.testng.Assert;
  import org.testng.ITestResult;
  import org.testng.annotations.AfterMethod;
  import org.testng.annotations.BeforeMethod;
  import org.testng.annotations.Parameters;
  import org.testng.annotations.Test;
  import 
  com.relevantcodes.extentreports.ExtentTest;
  import 
  com.relevantcodes.extentreports.LogStatus;
  
  public class SignInnTest extends TestBasee{
  
  
  
  SignInPagee lognObj;    
  ExtentTest logger;
  String elementName;
  String Comment;
  String expectedResult;
  String actualResult;
  
  @BeforeMethod(alwaysRun = true)
  @Parameters({ "Browser", "URL"})
  public void getBrowser(Method method, String Browser, String URL) throws Exception{
      logger = getReport().startTest((this.getClass().getSimpleName()+"::"+method.getName()), method.getName());
      logger.assignAuthor("VK");
      logger.assignCategory("SignInpage - Smoketesting and TextTesting--Live");
      homeObj = gm_OpenApp(Browser, URL);             
      lognObj = homeObj.navigateToSignInPage();           
  }
  
   @AfterMethod (alwaysRun = true)
      public void publishReport_SIP(ITestResult result) throws Exception{
          System.out.println("publishReport_SIP");
          String resultStatus = null;         
          if(result.getStatus() == ITestResult.FAILURE){              
              resultStatus = "FAILED"; 
              String screenshot_Path = gm_setScreenshotPath_forExtentReporting(elementName, resultStatus);            
              gm_TakeSnapshot(screenshot_Path);               
              String image = logger.addScreenCapture(screenshot_Path);
              logger.log(LogStatus.FAIL, Comment, image);
          }else{              
              resultStatus = "PASSED";                
              String screenshot_Path = gm_setScreenshotPath_forExtentReporting(elementName, resultStatus);
              gm_TakeSnapshot(screenshot_Path);
              System.out.println("screenshot_Path: "+screenshot_Path);
              String image = logger.addScreenCapture(screenshot_Path);
              logger.log(LogStatus.PASS, Comment, image);
          }
          getReport().endTest(logger);
          getReport().flush();
          System.out.println("closing now_SIP.");
          driverObj.quit();
      }        
  @Test (priority = 0, groups = {"Smoke""})
  public  void validateSignInPage_PageHeading_SIP() throws Exception{
      System.out.println("In validateSignInPage_PageHeading Method_SIP");                 
      elementName = "SignIn_PageHeading_txt";     
      Comment = "validatePageHeading_SignInpage";     
      actualResult =  "AUTHENTICATION";           
      expectedResult = "AUTHENTICATION";
      Assert.assertEquals(actualResult, expectedResult);  //Here test will pass
      System.out.println("Out  of validateSignInPageHeading method_SIP");
  }   
  
  @Test (groups = {"UITest"}, dependsOnMethods = { "validateSignInPage_PageHeading_SIP" })
  public  void validateSignInPage_SignInSectionHeading_SIP() throws Exception{
      System.out.println("In validateSignInPage_SignInSectionHeading Method_SIP");        
      elementName = "SignInPage_SignInSectionHeading_txt";        
      Comment = "validateSectionHeading_SignInpage";      
      actualResult =  "ALREADY REGISTERED1?"; 
      expectedResult = "ALREADY REGISTERED?";
      Assert.assertEquals(actualResult, expectedResult); //Here Test will fail as actual not equal to expected
      System.out.println("Out  of validateSignInPage_SignInSectionHeading method_SIP");
       }  
   }
  </code></pre>
  
  <p><b>6.testng.xml</b>
  suite name="Test" parallel = "tests" thread-count = "1">  </p>
  
  <pre><code>&lt;test name="CHTest"  &gt;   
      &lt;parameter name="Browser" value="CH" &gt;&lt;/parameter&gt;
      &lt;parameter name="URL" value="http://automationpractice.com/index.php"&gt;&lt;/parameter&gt;          
        &lt;groups&gt;&lt;run&gt;         
              &lt;include name="Smoke"/&gt;
              &lt;include name="UITest"/&gt;                 
          &lt;/run&gt;&lt;/groups&gt;             
      &lt;classes&gt;
          &lt;class name= "sampletestproject.SignInnTest" /&gt; 
          &lt;class name= "sampletestproject.HomepageeTest"/&gt;
      &lt;/classes&gt;&lt;/test&gt;&lt;/suite&gt;
  </code></pre>