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

來源:互聯網
上載者:User

標籤:nta   overflow   with   sub   遞迴   rom   就是   static   weather   

    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的原因

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.