本文介绍了使用静态 WebDriver 实例的 java.lang.NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的代码分成更小的函数.但是有一个问题,因为驱动程序并非对所有功能都可用.所以我将它声明为一个常量(或者有更好的方法吗?)

I want to separate my code in to smaller functions.But had an issue as driver was not available to all functions. So i declared it as a constant (or is there a better way of doing this ?)

但是在第三个函数中它在线失败:

but in 3rd function it is failing on line :

Select dropdown_finance_product = new Select(driver.findElement(By.xpath("//select[@id='ResultsNumber']")));

这是控制台消息:

Exception in thread "main" java.lang.NullPointerException
    at Scraping.scrapeit.fetch_urls(scrapeit.java:49)
    at Scraping.scrapeit.main(scrapeit.java:24)

代码:

package Scraping;

import java.io.IOException;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import tools.Xls_Reader;

public class scrapeit {

    static WebDriver driver;

    public static void main(String[] args) throws IOException {

        start_browser();
        fetch_urls();
        read_excel();
    }

    public static void start_browser() {

        System.setProperty("webdriver.chrome.driver", "C:\\Chrome Driver\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();

        driver.manage().deleteAllCookies();
        driver.get("http://www.example.com/search/items/new/");
        driver.manage().window().maximize();
        WebDriverWait wait = new WebDriverWait(driver, 20);
        wait.until(ExpectedConditions.titleContains("New items));
    }

    public static void fetch_urls() {

        Select dropdown_finance_product = new Select(driver.findElement(By.xpath("//select[@id='ResultsNumber']")));
        dropdown_finance_product.selectByVisibleText("100");
        System.out.println("Selected 100 Items Dropdown");

        // Open Excel and write urls back
        Xls_Reader datatable = new Xls_Reader("C:\\scrape.xlsx");

        List<WebElement> num_items = driver.findElements(
                By.xpath("//a [contains(@href,'http://www.example.com/search/items/new/latest-')] "));

        for (int i = 0; i < num_items.size(); i++) {
            System.out.println("How many items on page = : " + num_items.size() + " counter = " + i);

            String link = num_items.get(i).getAttribute("href");

            datatable.setCellData("New", "URL", i + 2, link);
            System.out.println("URL : " + link);
        }
    }

    public static void read_excel() {

        // Read in url and process...
        Xls_Reader datatable = new Xls_Reader("C:\\scrape.xlsx");
        int r = datatable.getRowCount("URL");
        int c = datatable.getColumnCount("URL");
        System.out.println("num of rows = " + r + " num of cols = " + c);
    }
}

推荐答案

错误说明了一切:

Exception in thread "main" java.lang.NullPointerException

class scrapeit 中,您已经将 webdriver 声明为 WebDriver 的一个实例为 static 如下:

In class scrapeit you have already declared webdriver as an instance of WebDriver as static as follows :

static WebDriver driver;

但是随后您又将另一个 driver 初始化为 WebDriver 的新实例,如下所示:

But then you are again initializing another driver as a new instance of WebDriver as follows :

WebDriver driver = new ChromeDriver();

因此你看到 java.lang.NullPointerException

一个快速的解决方案是,您需要使用 WebDriver静态实例,而不是创建 WebDriver 的另一个实例.所以你需要从 WebDriver driver = new ChromeDriver(); 中删除 WebDriver 如下:

A quick solution will instead of creating another instance of the WebDriver you need to use the static instance of WebDriver. So you need to remove WebDriver from WebDriver driver = new ChromeDriver(); as follows :

driver = new ChromeDriver();

这篇关于使用静态 WebDriver 实例的 java.lang.NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 10:02