Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在主类中调用不同的类方法时删除空指针异常?_Java_Selenium_Nullpointerexception - Fatal编程技术网

Java 如何在主类中调用不同的类方法时删除空指针异常?

Java 如何在主类中调用不同的类方法时删除空指针异常?,java,selenium,nullpointerexception,Java,Selenium,Nullpointerexception,在向主类调用不同的类方法时,我多次尝试删除空指针异常 public class Demo { WebDriver driver; int x; public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.chrome.driver", "D:\\Software\\chromed

在向主类调用不同的类方法时,我多次尝试删除空指针异常

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);
}
这是另一个我想在主类中调用的类

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());
                }
            }
        }
    }
}

为了不同的实践,我在主课上尝试了这个调用类的概念,但我一次又一次地面临同样的问题。我确信我在代码中遗漏了一些概念性的东西。

空指针意味着你试图访问一个没有(空)值的东西-基本上意味着它没有被实例化或设置

此时,您要在演示类中创建webdriver:

WebDriver driver = new ChromeDriver();
然后,当您执行此操作时:

Demo_Methods DM = new Demo_Methods(); **(Demo.java:31)**
DM.addCountryName();
您正在创建一个新的Demo_方法实例,但这不知道webdriver的价值


简单的解决方案是在使用构造函数创建演示方法时,在演示方法中传递并设置webdrvier

构造函数是在创建类的实例时调用的特殊方法

将此添加到您的演示方法中:

public Demo_Methods(WebDriver _driver){
driver = _driver;
}
这意味着,当您创建demo_方法时,您将webdriver传递给demo_方法,demo_方法知道它的价值

然后,您现在可以这样创建实例:

    Demo_Methods DM = new Demo_Methods(driver); 
    DM.addCountryName();

如果您可以发布stacktraces,这会有所帮助,但通常如果您试图捕获NPE,那么最好测试null/使用
可选的
,并引发不同的异常或返回不同的错误。打印出“NullPointerException抛出!”并没有真正的帮助。您正在屏蔽错误数据并丢失有关异常的所有信息。这是否回答了您的问题?演示方法不知道您的webdriver值是多少。创建收缩器并在创建时传入webdriver。
    Demo_Methods DM = new Demo_Methods(driver); 
    DM.addCountryName();