Java 使用ExtentReport获取空指针异常

Java 使用ExtentReport获取空指针异常,java,testng,Java,Testng,我有三节课。Test1、Test2和基类。Test1和Test2扩展了基类。当我使用TestNg xml执行这两个类时,@BeforeClass失败,出现空指针异常。正在生成test1的报告。也无程度报告其工作罚款。请帮帮我 ====================================================基类============================== public class Base { public ExtentReports reports; pu

我有三节课。Test1、Test2和基类。Test1和Test2扩展了基类。当我使用TestNg xml执行这两个类时,@BeforeClass失败,出现空指针异常。正在生成test1的报告。也无程度报告其工作罚款。请帮帮我

====================================================基类==============================

public class Base
{
public ExtentReports reports;
public ExtentTest logger;
  @BeforeClass

  public void startBrowser() 
  {
      String name= LocalDateTime.now().format(DateTimeFormatter.ofPattern("ddMMMYY_HHmmss"));
      logger = reports.createTest("Test1"+name);
      System.out.println("Started Test1");

}

  @BeforeSuite
  public void initialSetup()
  {
      String name= LocalDateTime.now().format(DateTimeFormatter.ofPattern("ddMMMYY_HHmmss"));
      ExtentHtmlReporter extent = new ExtentHtmlReporter(new File("./Reports/"+name+".html"));
      reports = new ExtentReports();
      reports.attachReporter(extent); 
  }

  @AfterClass
  public void end()
  {
      reports.flush();
  }

}
public class Test1 extends Base{
  @Test
  public void TestCase() 
  {

      logger.info("Test1");
  }
}
public class Test2 extends Base{
  @Test
  public void f() 
  {
      System.out.println("Started Test2");
  }
}
===========================================Test1类=====================

public class Base
{
public ExtentReports reports;
public ExtentTest logger;
  @BeforeClass

  public void startBrowser() 
  {
      String name= LocalDateTime.now().format(DateTimeFormatter.ofPattern("ddMMMYY_HHmmss"));
      logger = reports.createTest("Test1"+name);
      System.out.println("Started Test1");

}

  @BeforeSuite
  public void initialSetup()
  {
      String name= LocalDateTime.now().format(DateTimeFormatter.ofPattern("ddMMMYY_HHmmss"));
      ExtentHtmlReporter extent = new ExtentHtmlReporter(new File("./Reports/"+name+".html"));
      reports = new ExtentReports();
      reports.attachReporter(extent); 
  }

  @AfterClass
  public void end()
  {
      reports.flush();
  }

}
public class Test1 extends Base{
  @Test
  public void TestCase() 
  {

      logger.info("Test1");
  }
}
public class Test2 extends Base{
  @Test
  public void f() 
  {
      System.out.println("Started Test2");
  }
}
===============================Test2类=============================

public class Base
{
public ExtentReports reports;
public ExtentTest logger;
  @BeforeClass

  public void startBrowser() 
  {
      String name= LocalDateTime.now().format(DateTimeFormatter.ofPattern("ddMMMYY_HHmmss"));
      logger = reports.createTest("Test1"+name);
      System.out.println("Started Test1");

}

  @BeforeSuite
  public void initialSetup()
  {
      String name= LocalDateTime.now().format(DateTimeFormatter.ofPattern("ddMMMYY_HHmmss"));
      ExtentHtmlReporter extent = new ExtentHtmlReporter(new File("./Reports/"+name+".html"));
      reports = new ExtentReports();
      reports.attachReporter(extent); 
  }

  @AfterClass
  public void end()
  {
      reports.flush();
  }

}
public class Test1 extends Base{
  @Test
  public void TestCase() 
  {

      logger.info("Test1");
  }
}
public class Test2 extends Base{
  @Test
  public void f() 
  {
      System.out.println("Started Test2");
  }
}

问题在于您的测试代码。 您已经在
@BeforeSuite
注释方法中初始化了数据成员
报告

每个
标记只执行一次。 由于两个类都是从同一基类扩展而来,因此
initialSetup()
方法将只执行一次(例如,Test1.java)

这就是为什么当您的
@BeforeClass
注释的
startBrowser()
尝试访问
reports.createTest(“Test1”+name)时
对于
Test2
您遇到了NullPointerException

您需要通过从
@BeforeClass
方法中移动
initialSetup()
调用来更改此设置