zoukankan      html  css  js  c++  java
  • 自己遇到过的出现java.lang.StackOverflowError的原因

        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那么这个就会无限循环下去,就会在栈中不断调用方法,直至栈溢出。

  • 相关阅读:
    【UOJ Round #3】
    【UOJ Round #1】
    【BZOJ】【3673】可持久化并查集 & 【3674】可持久化并查集加强版
    【ContestHunter】【弱省胡策】【Round7】
    【BZOJ】【3211】花神游历各国
    【BZOJ】【4146】 【AMPPZ2014】Divisors
    【BZOJ】【3931】【CQOI2015】网络吞吐量
    【BZOJ】【3697】采药人的路径&【3127】【USACO2013 Open】Yin and Yang
    【BZOJ】【3930】【CQOI2015】选数
    【ContestHunter】【弱省胡策】【Round6】
  • 原文地址:https://www.cnblogs.com/wth21-1314/p/9288269.html
Copyright © 2011-2022 走看看