I am getting Null Pointer exception when using the selenium script in katalon recorder using interlliJ

Please check and let me know what have to change.
package SpinConnect;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;

import java.util.concurrent.TimeUnit;

import static org.testng.Assert.fail;
import java.util.concurrent.TimeUnit;
import org.testng.annotations.;
import static org.testng.Assert.
;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class Test
{
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
private JavascriptExecutor js;

@BeforeClass(alwaysRun = true)
public static void main(String[] args)
{
    WebDriverManager.chromedriver().setup();
    WebDriver driver = new ChromeDriver();
    driver.get("http://182.76.43.166:9000/");
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    JavascriptExecutor js = (JavascriptExecutor) driver;
        }

        @org.testng.annotations.Test
        public void testSpinConnect() throws Exception {
            driver.findElement(By.id("uname")).sendKeys("lmwrnd");
            driver.findElement(By.id("pswd")).sendKeys("may@2020");
            driver.findElement(By.xpath("//button[@type='submit']")).click();
            driver.findElement(By.cssSelector("path")).click();
            driver.findElement(By.linkText("Production")).click();
            driver.findElement(By.cssSelector("path")).click();
            driver.findElement(By.linkText("Reports Generate ReportsWaste Management ReportWrite LoggerExecutive ReportFavourite ListsPlant Report")).click();
            driver.findElement(By.linkText("Waste Management Report")).click();
            driver.findElement(By.cssSelector("path")).click();
            driver.findElement(By.linkText("Analysis YBS Spindle Live StatusYBS Spindle HistoryYBS Cop AnalysisYBS Doff AnalysisPower AnalysisHealth Monitoring AnalysisAir Consumption Analysis")).click();
            driver.findElement(By.linkText("YBS Doff Analysis")).click();
            driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='Define Escalations'])[1]/preceding::*[name()='svg'][2]")).click();
            driver.findElement(By.linkText("Maintenance DashboardTicket ListsAdd TicketPlace OrderERPOrdersLog Time")).click();
            driver.findElement(By.linkText("Add Ticket")).click();
            driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='Define Escalations'])[1]/preceding::*[name()='svg'][2]")).click();
            driver.findElement(By.linkText("Bale Management Bale Template managementBale ReceiptsBale Test ReportsBale Stock ReportBale Distribution")).click();
            driver.findElement(By.linkText("Bale Receipts")).click();
            driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='Define Escalations'])[1]/preceding::*[name()='svg'][2]")).click();
            driver.findElement(By.xpath("//p[@id='alert-dialog-slide-description']/ul/div/a/span[2]")).click();
        }

        @AfterClass(alwaysRun = true)
        public void tearDown() throws Exception {
            driver.quit();
            String verificationErrorString = verificationErrors.toString();
            if (!"".equals(verificationErrorString)) {
                fail(verificationErrorString);
            }
        }

        private boolean isElementPresent(By by) {
            try {
                driver.findElement(by);
                return true;
            } catch (NoSuchElementException e) {
                return false;
            }
        }

        private boolean isAlertPresent() {
            try {
                driver.switchTo().alert();
                return true;
            } catch (NoAlertPresentException e) {
                return false;
            }
        }

        private String closeAlertAndGetItsText() {
            try {
                Alert alert = driver.switchTo().alert();
                String alertText = alert.getText();
                if (acceptNextAlert) {
                    alert.accept();
                } else {
                    alert.dismiss();
                }
                return alertText;
            } finally {
                acceptNextAlert = true;
            }
        }
    }

Exported script from katalon:
package com.example.Trial;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.testng.annotations.;
import static org.testng.Assert.
;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import java.io.File;
import org.apache.commons.io.FileUtils;

public class SpinConnect {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
private JavascriptExecutor js;

@BeforeClass(alwaysRun = true)
public void setUp() throws Exception {
System.setProperty(“webdriver.chrome.driver”, “”);
driver = new ChromeDriver();
baseUrl = “https://www.google.com/”;
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
js = (JavascriptExecutor) driver;
}

@Test
public void testSpinConnect() throws Exception {
driver.get(“http://182.76.43.166:9000/”);
driver.findElement(By.id(“uname”)).click();
driver.findElement(By.id(“uname”)).clear();
driver.findElement(By.id(“uname”)).sendKeys(“lmwrnd”);
driver.findElement(By.id(“pswd”)).clear();
driver.findElement(By.id(“pswd”)).sendKeys(“may@2020”);
driver.findElement(By.xpath(“//button[@type=‘submit’]”)).click();
driver.findElement(By.cssSelector(“path”)).click();
driver.findElement(By.linkText(“Production”)).click();
driver.findElement(By.cssSelector(“path”)).click();
driver.findElement(By.linkText(“Reports Generate ReportsWaste Management ReportWrite LoggerExecutive ReportFavourite ListsPlant Report”)).click();
driver.findElement(By.linkText(“Waste Management Report”)).click();
driver.findElement(By.cssSelector(“path”)).click();
driver.findElement(By.linkText(“Analysis YBS Spindle Live StatusYBS Spindle HistoryYBS Cop AnalysisYBS Doff AnalysisPower AnalysisHealth Monitoring AnalysisAir Consumption Analysis”)).click();
driver.findElement(By.linkText(“YBS Doff Analysis”)).click();
driver.findElement(By.xpath(“(.//[normalize-space(text()) and normalize-space(.)=‘Define Escalations’])[1]/preceding::[name()=‘svg’][2]”)).click();
driver.findElement(By.linkText(“Maintenance DashboardTicket ListsAdd TicketPlace OrderERPOrdersLog Time”)).click();
driver.findElement(By.linkText(“Add Ticket”)).click();
driver.findElement(By.xpath(“(.//[normalize-space(text()) and normalize-space(.)=‘Define Escalations’])[1]/preceding::[name()=‘svg’][2]”)).click();
driver.findElement(By.linkText(“Bale Management Bale Template managementBale ReceiptsBale Test ReportsBale Stock ReportBale Distribution”)).click();
driver.findElement(By.linkText(“Bale Receipts”)).click();
driver.findElement(By.xpath(“(.//[normalize-space(text()) and normalize-space(.)=‘Define Escalations’])[1]/preceding::[name()=‘svg’][2]”)).click();
driver.findElement(By.xpath(“//p[@id=‘alert-dialog-slide-description’]/ul/div/a/span[2]”)).click();
}

@AfterClass(alwaysRun = true)
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!“”.equals(verificationErrorString)) {
fail(verificationErrorString);
}
}

private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}

private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}

private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
}

Please reply me as soon as possible.

1 Like

I’m just taking a quick look at your code and I see a few things that may assist you.

  1. you duplicate the following two import statements at the top of your code:
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
  1. you identify a variable, driver, in your class, Test, as:
    private WebDriver driver;
    but you declare and instantiate another object in your method main(String[] args), with the same name (but is NOT the same object) through the declaration:
    WebDriver driver = new ChromeDriver();
    Because of the way you declared and initialized this variable, the variable is also scoped to just the method “main()” and is NOT available to the rest of your program. Change the initialization of driver above to just:
    driver = new ChromeDriver();
    and then it should be available because of your first private declaration.
  2. With the above change, then your variable, driver, will become initialized for use in your method, testSpinConnect(). How your code is now, I don’t see how driver in this method can be initialized and therefore could be your NullPointer source.

Edit: “scoped” means there is a limited range that the variable can be used.

I will rectify issues as per your suggestions.
Thank you so much Mike. And also, thank you for the kind response.