2012-06-25 111 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