0

I am trying to create an executable jar file of a Maven Project in Eclipse.was using testNG in the maven project..so created a runner class with main method and exported runnable Jar File..Now it's not Running .. i feel it's not reading datas from Excel which i put into the project...

Java version enter image description here

Dependencies enter image description here

My RunnerClass with main Method code

@SuppressWarnings("deprecation")
static TestNG tng;

@SuppressWarnings("deprecation")
public static void main(String[] args) {
    
    ExtentReportListener ext = new ExtentReportListener();
    
    tng = new TestNG();
    
    tng.setTestClasses(new Class[] {Login.class});
    tng.addListener(ext);
    tng.run();
}

The test class which i want to execute looks like:

@BeforeTest
    public void setup() throws IOException {
        
        initialization();
        loginPage = new LoginPage();
        homepage = new HomePage();
        
    }
    
    
    @Test(priority=1,groups = {"Regression","UnitTest"})
    public void Titlecheck() {
        
        String title = driver.getTitle();
        System.out.println(title);
        Assert.assertEquals(title, "Login pagina");
        
    }
    
    
    @Test(priority=2,enabled=true)
    public void login() throws InterruptedException, IOException {
        
        loginPage.ClearUsPwd();
        loginPage.LoginPageTest(getData("Logindata", 1, 1),getData("Logindata", 1, 2));

    }

The Initialization method and getData method are in baseclass

public static void initialization() throws IOException {
    WebDriverManager.chromedriver().setup();
    driver = new ChromeDriver();
    driver.get(getData("login", 0, 1  ));
    driver.manage().window().maximize();
}

public static String getData(String sheetName ,int rowNo, int cellNo) throws IOException {
    
    File f = new File("./resources/excelData/Sample.xlsx");
    FileInputStream fis = new FileInputStream(f);
    XSSFWorkbook w = new XSSFWorkbook(fis);
     XSSFSheet sheet = w.getSheet(sheetName);
     Row r = sheet.getRow(rowNo);
     Cell c =r.getCell(cellNo ,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
     String value = c.getStringCellValue();

return value; }

While running it in cmd The exception i get below

Test Suite started! Titlecheck skipped! java.lang.NullPointerException at base.Testbase.getScreenshot(Testbase.java:107) at extentReport.ExtentReportListener.onTestSkipped(ExtentReportListener.java:121) at org.testng.internal.Invoker.runTestListeners(Invoker.java:1723) at org.testng.internal.Invoker.runTestListeners(Invoker.java:1714) at org.testng.internal.Invoker.registerSkippedTestResult(Invoker.java:1272) at org.testng.internal.Invoker.invokeMethod(Invoker.java:605) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) at org.testng.TestRunner.privateRun(TestRunner.java:744) at org.testng.TestRunner.run(TestRunner.java:602) at org.testng.SuiteRunner.runTest(SuiteRunner.java:380) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340) at org.testng.SuiteRunner.run(SuiteRunner.java:289) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301) at org.testng.TestNG.runSuitesLocally(TestNG.java:1226) at org.testng.TestNG.runSuites(TestNG.java:1144) at org.testng.TestNG.run(TestNG.java:1115) at testScenarios.RunnerClass.main(RunnerClass.java:21) login skipped! java.lang.NullPointerException at base.Testbase.getScreenshot(Testbase.java:107) at extentReport.ExtentReportListener.onTestSkipped(ExtentReportListener.java:121) at org.testng.internal.Invoker.runTestListeners(Invoker.java:1723) at org.testng.internal.Invoker.runTestListeners(Invoker.java:1714) at org.testng.internal.Invoker.registerSkippedTestResult(Invoker.java:1272) at org.testng.internal.Invoker.invokeMethod(Invoker.java:605) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) at org.testng.TestRunner.privateRun(TestRunner.java:744) at org.testng.TestRunner.run(TestRunner.java:602) at org.testng.SuiteRunner.runTest(SuiteRunner.java:380) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340) at org.testng.SuiteRunner.run(SuiteRunner.java:289) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301) at org.testng.TestNG.runSuitesLocally(TestNG.java:1226) at org.testng.TestNG.runSuites(TestNG.java:1144) at org.testng.TestNG.run(TestNG.java:1115) at testScenarios.RunnerClass.main(RunnerClass.java:21) Test Suite is ending!

=============================================== Command line suite Total tests run: 2, Failures: 0, Skips: 2 Configuration Failures: 1, Skips: 0

The NullPointerException thrown Line below

public String getScreenshot() {  **106th Line**
    File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);**107th Line**
    String path = System.getProperty("user.dir") + "/screenshots/" + System.currentTimeMillis() + ".png";
    File destination = new File(path);
    try {
        FileUtils.copyFile(src, destination);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return path;
}
  • 1
    You probably want to provide more details here... I think the most common cause would be in your project configuration... SDK, JRE versions/targets, dependency settings, etc... include those details. Also, if you can see the exception include the full text of it. – pcalkins May 13 '22 at 19:40
  • Am a novice to this and i think this is what you are asking.. – Achilles 1403 May 14 '22 at 04:55
  • The `NPE` is in `Testbase.java:107`. So what code line is code line `107` in `Testbase.java`? And what code initializes the objects used in that code line? Because one of those objects is `null`. And this is the reason of that `NPE`. – Axel Richter May 14 '22 at 14:51
  • The Null Pointer is Due to ScreenShot which couldnt happen because the tests are skipped....But the Actual problem (IN THE JAR EXECUTION) it couldnt read the Excel file..Whats the thing required to read the excel in the jar execution..Whats the Missing Point... – Achilles 1403 May 14 '22 at 15:15
  • "Whats the thing required to read the excel": It needs access to the file `./resources/excelData/Sample.xlsx`.Then `XSSFWorkbook` gets created. But else `new XSSFWorkbook(fis)` would throw `IOException` **before** the `NPE`. You should not assuming what happens but following the exceptions. Seems `driver` is `null` in `File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE)`. So try to detect **why** `driver` is null. There should be exceptions if `driver` is not `null` in normal program flow but is now. – Axel Richter May 14 '22 at 16:18
  • To put you to the right direction: Using file paths like `"./resources/excelData/Sample.xlsx"` is not good in productive environments because it is unclear what directory `.` points to. It may get used in examples but in productive code one should get resorces using `Class.getResourceAsStream` or `ClassLoader.getResourceAsStream`. But, as said, there must be other problems with your code like suppressing exceptions. Else `new XSSFWorkbook` would throw `IOException`. – Axel Richter May 15 '22 at 05:00
  • Thanks Axel Richter for your help...I should look at the areas you have pointed... – Achilles 1403 May 15 '22 at 08:34
  • Also the NullPointer is due to driver being Null in getScreenshot method...i have the initialization method and getScreenshot method in baseclass(declaring WebDriver driver public static) ...called the getscreenshot method in ExtentReportListener Class... – Achilles 1403 May 15 '22 at 08:48
  • The `NPE` get thrown by that code line. So something mus be `null` there. What else as `driver` should it be? Or is there a `Caused by` in the stacktrace and you have not shown this? Then, of course my assumtion can be wrong. Please show the full stacktrace. – Axel Richter May 16 '22 at 06:48
  • this'll depend on how you call the getscreenshot method... sounds like it's being called from a static class... it's not an injected reference to a class object. Just to confirm that the driver is null, run a null check and output if it's null. Ex: if (driver!=null) { system.out.println("driver is not null"); ...take screenshot... } else {system.out.println("driver is null");} – pcalkins May 19 '22 at 22:52

0 Answers0