I tried many times to remove null pointer exception while calling a different class method into main class.
public class Demo {
WebDriver driver;
int x;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "D:\\Software\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
String Url = "https://www.worldometers.info/coronavirus/#countries";
driver.get(Url);
Thread.sleep(5000);
Demo_Methods DM = new Demo_Methods(); **(Demo.java:31)**
DM.addCountryName();
System.out.println(DM.Countries);
}
Here is the another class which I want to call in main class
public class Demo_Methods
{
public WebDriver driver;
public List<String> Countries = new ArrayList<String>();
public List<String> TotalCases = new ArrayList<String>();
public List<String> NewCases = new ArrayList<String>();
public List<String> TotalDeaths = new ArrayList<String>();
public void addCountryName() throws InterruptedException
{
//List<WebElement> Count = driver.findElements(By.xpath(("//table[@id='main_table_countries_today']//tbody[1]//tr")));
for(int z=1;z<12;z++)
{
List<WebElement> CountryName = driver.findElements(By.xpath(("//table[@id='main_table_countries_today']//tbody[1]//tr["+z+"]//td")));
//System.out.println(CountryName.size());
Thread.sleep(5000);
for(int x=2;x<6;x++)
{
if(CountryName.get(1).getText().isEmpty())
{
break;
}
else {
WebElement Country=driver.findElement(By.xpath(("//table[@id='main_table_countries_today']//tbody[1]//tr["+x+"]//td[2]")));
{
if(Country.getText().isEmpty())
{
Countries.add("000");
break;
}
else {
Countries.add(Country.getText());
}
}
WebElement TotalCase =driver.findElement(By.xpath(("//table[@id='main_table_countries_today']//tbody[1]//tr["+x+"]//td[3]")));
if(TotalCase.getText().isEmpty())
{
TotalCases.add("000");
break;
}
else {
TotalCases.add(TotalCase.getText());
}
}
WebElement NewCase =driver.findElement(By.xpath(("//table[@id='main_table_countries_today']//tbody[1]//tr["+x+"]//td[4]")));
if(NewCase.getText().isEmpty())
{
NewCases.add("000");
break;
}
else {
NewCases.add(NewCase.getText());
}
WebElement TotalDeath =driver.findElement(By.xpath(("//table[@id='main_table_countries_today']//tbody[1]//tr["+x+"]//td[5]")));
if(TotalDeath.getText().isEmpty())
{
TotalDeaths.add("000");
break;
}
else {
TotalDeaths.add(TotalDeath.getText());
}
}
}
}
}
Exception in thread "main" java.lang.NullPointerException at myInfoModule.Demo_Methods.addCountryName(Demo_Methods.java:23) at myInfoModule.Demo.main(Demo.java:31)
I tried this call concept of class in the main class for different practices but I faced the same issues again and again. I'm sure that I had missed some conceptual thing to attach in my code.