2012-06-25 80 views
0

因此,我筛选了一些代码,并且无法排除此空指针异常错误。空指针异常与Jsoup解析

我试图解析从源代码行一表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代码是正确的,但我本来可以有选择困惑,节点元素以及查询。如果任何人都可以帮助它将不胜感激。

+1

关于第72行,如果这是我的代码,我会打破长链方法,并将每个方法调用放在它自己的行上,看看哪个方法调用导致NPE更好。还要考虑使用调试语句。 –

+0

我分手了,它仍然说72行。我曾尝试使用toString在每个间隔打印出来,他们都失败了。 – user1093111

+0

这会让我相信它与getElementById有关,但它没有意义 – user1093111

回答

0

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

通过评价来看,似乎doc.getElementById("fundamentalsForm")正在生成NPE。

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

+0

我还没有检查过,我不知道该怎么做,我知道KO已经注册了,它是可乐,但我确实发现用户名和密码f orm实际上是userid和密码。 – user1093111

+0

我在想你是对的,但我不知道如何找出程序要进入的页面。 – user1093111