-1

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.

David Buck
  • 3,752
  • 35
  • 31
  • 35
  • 1
    If you can post the stacktraces that would help, but generally if you're trying to catch NPE's, you're better off testing for null/using an ```Optional```, and throwing a different exception or returning a different error. Printing out "NullPointerException thrown!" doesn't really help. You're masking bad data and losing everything about the exception. – Gryphon Aug 30 '20 at 06:23
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Thiyagu Aug 30 '20 at 06:27
  • Demo methods doesn't know what your webdriver value is. Create a constrictor and pass in webdriver when you create. – RichEdwards Aug 30 '20 at 07:22

1 Answers1

0

Null Pointer means you're trying to access a something which has no (a null) value - basically meaning it's not been instantiated or set.

At the moment you do this to create your webdriver in your Demo class:

WebDriver driver = new ChromeDriver();

Then, when you do this:

Demo_Methods DM = new Demo_Methods(); **(Demo.java:31)**
DM.addCountryName();

You're creating a new instance of Demo_Methods - but this has NO idea what your VALUE of webdriver is.


The simple solution is to pass and set webdrvier in your demo_methods when you create demo methods with a constructor.

A constructor is special method that is called when you create an instance the class.

Add this to your Demo_Methods:

public Demo_Methods(WebDriver _driver){
driver = _driver;
}

This means, when you create demo_methods, you pass in your webdriver to demo_methods knows the value of it.

Then, you now create your instance like this:

    Demo_Methods DM = new Demo_Methods(driver); 
    DM.addCountryName();
RichEdwards
  • 3,423
  • 2
  • 6
  • 22