1. 程式人生 > >自己遇到過的出現java.lang.StackOverflowError的原因

自己遇到過的出現java.lang.StackOverflowError的原因

方法 .get obj jsonarray json ack getjson 出現 equals

public static JSONArray geth24Weather(String result) {//獲取當天24小時以及第二天的天氣結果對象
JSONObject fromObject = null;
JSONArray h24weatherArray =null;
try {
int indexOf = result.indexOf("{");
int lastIndexOf = result.lastIndexOf("}");
result=result.substring(indexOf, lastIndexOf+1);
String replaceAll = result.replaceAll(" ", "");

if(replaceAll.trim().startsWith("{")){
fromObject = JSONObject.fromObject(result);
}
//System.out.println("fromObject"+fromObject);
} catch (Exception e) {
e.printStackTrace();
}
if(fromObject!=null&&fromObject.get("code").equals("0")){
JSONArray jsonArray = fromObject.getJSONArray("data");//獲取天氣數據對象
JSONArray forecastArray= ((JSONObject)jsonArray.get(0)).getJSONArray("forecast");//獲取預報天氣對象
JSONObject todayObj= (JSONObject)forecastArray.get(0);//獲取到當天對象
if(todayObj.containsKey("h24weather")&&todayObj.get("h24weather")!=null){
System.out.println("打印todayObj="+todayObj);
h24weatherArray=todayObj.optJSONArray("h24weather");
if(h24weatherArray==null){


geth24Weather(result);
}
System.out.println("打印h24weatherArray="+h24weatherArray);
}else{

System.out.println("空");
}

}
return h24weatherArray;
}

這個就是遞歸沒有結束條件,一旦遇到result是null那麽這個就會無限循環下去,就會在棧中不斷調用方法,直至棧溢出。

自己遇到過的出現java.lang.StackOverflowError的原因