0

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

我正在尝试从源代码行 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 元素以及查询感到困惑。如果有人可以提供帮助,将不胜感激。

4

1 回答 1

0

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

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

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

于 2012-06-27T15:22:19.640 回答