为什么我的代码会出现 java.lang.StackOverflowError 错误?

4
在我的Java类中,我有一个名为getProperties()static方法,它返回java.util.Properties
在另一个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() 方法会不断被调用,而没有到达返回语句。

2
不直接相关:将 fis.close(); 移动到(不存在的)finally子句中。如果出现异常,您将拥有打开的 FileInputStream - Emz
2
你能分享一下那个文件的内容吗?也许里面有些奇怪的东西? - Mureinik
你在使用Maven吗?因为那看起来像是一个Maven化的路径。 - Elliott Frisch
我正在使用Java 7。是的,我正在使用Maven。当我尝试将fis.close();移动到finally块中并运行它时,但没有成功。 - The Guest
@Mureinik,能否告诉我文件的内容是什么? - The Guest
显示剩余5条评论
2个回答

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
这与他们现有的有何不同?这会带来什么不同?为什么你建议这样做? - Sotirios Delimanolis
一个文件的内容如何导致StackOverflowError - Sotirios Delimanolis
我在想什么?我要求你澄清你的回答,因为我不明白它与问题的相关性。你的代码基本上和他们的一样。你关于StackOverflowError的建议,乍一看似乎是不可能的。请澄清你的回答。 - Sotirios Delimanolis

-1
StackOverflowError发生是因为你的代码多次调用了一个静态方法,你能贴出整个代码吗?这样我们就可以看到为什么getProperties方法没有达到return语句。谢谢。

在调试模式下,getProperties() 方法会不断被调用,而且没有到达 return 语句。因此我认为调用 getProperties 的静态方法存在错误的可能性。 - PhstKv

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接