2015-12-09 31 views
4

在我的java類中,我有一個名爲getProperties()static方法,返回java.util.Properties爲什麼我在代碼中得到java.lang.StackOverflowError?

在另一個static方法,我調用該方法,如:

Properties p = getProperties(); 

而且getProperties()方法:

private static Properties getProperties(){ 
     Properties properties = new Properties(); 
     try{   
      InputStream fis = null;     
      fis = new FileInputStream("src/main/resources/fileName.properties"); //In DEBUG mode control comes until here and returns to Properties p = getProperties(); in the calling method every time continuously 
      properties.load(fis); 
      fis.close();    
     }catch(Exception e){ 
      //...... 
     } 
     return properties; 
    } 

錯誤:

Exception in thread "main" java.lang.StackOverflowError 
    at sun.misc.VM.isBooted(VM.java:165) 
    at java.util.Hashtable.initHashSeedAsNeeded(Hashtable.java:226) 
    at java.util.Hashtable.<init>(Hashtable.java:263) 
    at java.util.Hashtable.<init>(Hashtable.java:283) 
    at java.util.Properties.<init>(Properties.java:143) 
    at java.util.Properties.<init>(Properties.java:135) 

雖然在調試模式下的GetProperties() ;方法被連續調用而沒有達到return語句。

+0

你正在使用哪個Java版本? – Kayaman

+2

不直接相關:將'fis.close();'移至(不存在的!)'finally'子句。如果您遇到異常,您將打開FileInputStream。 – Emz

+2

你能分享該文件的內容嗎?那裏可能會有些時髦嗎? – Mureinik

回答

0

我執行了代碼它沒問題,但我認爲錯誤Exception in thread "main" java.lang.StackOverflowError,也許有什麼會導致問題。發生

Properties p = new Properties(); 
FileInputStream fs = new FileInputStream("src/main/resources/fileName.properties")); 
p.load(fs); 
+1

這與他們有什麼不同?這會有什麼不同?你爲什麼暗示它? –

+0

文件內容如何導致StackOverflowError? –

+0

我對什麼有什麼想法?我要求你澄清你的答案,因爲我不明白它的相關性。你的代碼和他們的代碼非常相似。乍一看,你對StackOverflowError的建議似乎是不可能的。請澄清你的答案。 –

-1

的StackOverflowError因爲你的代碼是調用的次數靜態方法的多個號碼,您可以張貼整個代碼,以便我們可以看到爲什麼的GetProperties方法沒有達到回報?謝謝

+0

「在調試模式下getProperties();方法被連續調用而沒有達到return語句」。所以我相信調用getProperties的靜態方法很容易出錯 – PhstKv

相關問題