如何解决如何在调用不同的类方法到主类时删除空指针异常?
我尝试了多次删除空指针异常的方法,同时在主类中调用了另一个类方法。
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());
}
}
}
}
}
Exception in thread "main" java.lang.NullPointerException at myInfoModule.Demo_Methods.addCountryName(Demo_Methods.java:23) at myInfoModule.Demo.main(Demo.java:31)
我在主班尝试了这种叫课的概念,以作不同的练习,但我一次又一次地遇到相同的问题。我确定我错过了一些要在代码中附加的概念性内容。
解决方法
空指针意味着您正在尝试访问没有值(空值)的东西-基本上意味着它没有被实例化或设置。
目前,您正在执行此操作以在Demo类中创建Webdriver:
WebDriver driver = new ChromeDriver();
然后,当您执行此操作时:
Demo_Methods DM = new Demo_Methods(); **(Demo.java:31)**
DM.addCountryName();
您正在创建一个Demo_Methods的新实例-但这不知道您的webdriver值是什么。
简单的解决方案是在使用构造函数创建演示方法时在demo_method中传递并设置webdrvier。
构造函数是一种特殊的方法,当您创建类的实例时会调用它。
将此添加到您的Demo_Methods:
public Demo_Methods(WebDriver _driver){
driver = _driver;
}
这意味着,当您创建demo_methods时,您将Web驱动程序传递给demo_methods知道它的价值。
然后,您现在像这样创建实例:
Demo_Methods DM = new Demo_Methods(driver);
DM.addCountryName();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。