【问题标题】:Null Pointer Exception with Jsoup ParseJsoup Parse 的空指针异常
【发布时间】:2016-02-10 23:35:58
【问题描述】:

所以我筛选了一些代码,无法解决这个空指针异常错误。

我正在尝试从源代码行 2290 到 3153 http://pastebin.com/DjGHED5t 解析表格

但是,在我的一个 CSS 查询中,代码失败并且对我来说毫无意义。

public void updateCompanyIs()throws IOException{
    investoolsLogin();

    Document doc = Jsoup.connect("http://toolbox.investools.com/graphs/fundamentalAnalysis.iedu?report=BS&symbol="+(Ticker)).get();
    // Elements table = doc.select("table");
/**LINE 72**/ 
    Elements columns = doc.getElementById("fundamentalsForm").children().select("table").get(0).select("tr").get(0).select("td");
    Iterator<Element> columnIterator = columns.iterator();
    int col = 0;
    int row = 0;
        while (columnIterator.hasNext()) {
            Element column = columnIterator.next();
            Elements rows = column.select("table").get(0).select("tr");
            Iterator<Element> rowsIterator = rows.iterator();
            col = col + 1;
            while (rowsIterator.hasNext()){
                row = row + 1;
                //Element rowIterator.next = ;
                incomeStatementInfo[col][row] = rowsIterator.next();
            }
        }
    }

    public void updateCompanyBs()throws IOException{
        investoolsLogin();

        Document doc = Jsoup.connect("http://toolbox.investools.com/graphs/fundamentalAnalysis.iedu?report=BS&symbol="+(Ticker)).get();
        // Elements table = doc.select("table");
        Elements columns = doc.getElementById("fundamentalsForm").children().select("table").get(0).select("tr").get(0).select("td");
        Iterator<Element> columnIterator = columns.iterator();
        int col = 0;
        int row = 0;
        while (columnIterator.hasNext()) {
            Element column = columnIterator.next();
            Elements rows = column.select("table").get(0).select("tr");
            Iterator<Element> rowsIterator = rows.iterator();
            col = col + 1;
            while (rowsIterator.hasNext()){
                row = row + 1;
                //Element rowIterator.next = ;
                balanceSheetInfo[col][row] = rowsIterator.next();
            }
        }
    }   

    public void updateCompanyCf()throws IOException{
        investoolsLogin();

        Document doc = Jsoup.connect("http://toolbox.investools.com/graphs/fundamentalAnalysis.iedu?report=BS&symbol="+(Ticker)).get();
        // Elements table = doc.select("table");
        Elements columns = doc.getElementById("fundamentalsForm").children().select("table").get(0).select("tr").get(0).select("td");
        Iterator<Element> columnIterator = columns.iterator();
        int col = 0;
        int row = 0;
        while (columnIterator.hasNext()) {
            Element column = columnIterator.next();
            Elements rows = column.select("table").get(0).select("tr");
            Iterator<Element> rowsIterator = rows.iterator();
            col = col + 1;
            while (rowsIterator.hasNext()){
                row = row + 1;
                //Element rowIterator.next = ;
                cashFlowsInfo[col][row] = rowsIterator.next();
            }
        }
    }

    public void updateCompanyInfo(String Ticker) throws IOException {
    /** LINE 134**/ 
        updateCompanyIs();
        updateCompanyBs();
        updateCompanyCf();

    }
}

这是错误:

Exception in thread "main" java.lang.NullPointerException
    at Company.updateCompanyIs(Company.java:72)
    at Company.updateCompanyInfo(Company.java:134)
    at Company.<init>(Company.java:41)
    at AppGUI.main(AppGUI.java:124)

这是我的 AppGUI:

public static void main(String[] args) throws Exception{
    Company company = new Company("KO"); // Creates new Company. Updating methods are called from constructor automatically.
    AppGUI frame = new AppGUI(company); // Creates new App GUI.  Various panes are initialized from constructor.
    frame.retrieveGUI(company);
    frame.setTitle("Financial Calculator | Ratios");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setMinimumSize(new Dimension(1000, 500));
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

我认为我的 JSOUP 代码是正确的,但我可能对 select 和 node 元素以及查询感到困惑。如果有人可以提供帮助,将不胜感激。

【问题讨论】:

  • 关于第 72 行,如果这是我的代码,我会打破长链方法行并将每个方法调用放在自己的行上,以便更好地查看导致 NPE 的方法调用。也可以考虑使用调试语句。
  • 我把它拆了,它仍然说第 72 行。我尝试使用 toString 打印到每个时间间隔,但都失败了。
  • 这会让我相信它与 getElementById 有关,但它没有意义
  • 您在使用之前检查过doc是否为空吗? System.out.println("doc is null: " + (doc == null)); 之类的东西?
  • 您确定您已通过investools.com 上的应用程序进行身份验证吗?

标签: java html arrays jsoup


【解决方案1】:

第 72 行:Elements columns = doc.getElementById("fundamentalsForm").children().select("table").get(0).select("tr").get(0).select("td");

从 cmets 看来,doc.getElementById("fundamentalsForm") 似乎正在生成 NPE。

我怀疑每个有效页面都有“fundamentalsForm”表单,所以这不应该是问题。您是否检查并确认您没有触发错误页面(即股票代码未注册)?

【讨论】:

  • 我没有检查,我不确定我会怎么做。我知道KO已经注册。是可乐。但是我确实发现用户名和密码表单的名称实际上是用户名和密码。
  • 我认为您是对的,但我不知道如何找出程序将转到哪个页面。
猜你喜欢
  • 1970-01-01
  • 2015-07-12
相关资源
最近更新 更多