2016-12-19 1 views
-1

나는 안드로이드 프로젝트에서 일하고있다. 그래프를 작성하고 그래프에 json 컨텐츠로 채 웁니다.

제 문제는이 오류가 계속 발생하고 이유가 확실하지 않습니다. java.lang.NegativeArraySizeException : -2왜 java.lang.NegativeArraySizeException : -2, 확실하지 이유

My Log.v는 배열의 내용을 표시합니다. 그래서 그것은 비어 있지 않습니다. 어쩌면 나는 뭔가를 놓치고있다.

나는 나머지 API를 통해 arraylist resModelList에 모든 것을 추가합니다. onPostExecute에서이 배열 목록 yVals1에 y 축 값을 추가하고 싶습니다. 여기는 내 오류가 발생합니다. (java.lang.NegativeArraySizeException : -2)

이렇게 값을 추가하면 오류가 발생하지 않습니다.

yVals1 = new ArrayList<Entry>(); 
    yVals1.add(new Entry(1451606400, 10)); 
    yVals1.add(new Entry(1454284800, 20)); 
    yVals1.add(new Entry(1456790400, 30)); 
    yVals1.add(new Entry(1459468800, 50)); 

내 코드
전역 변수

ArrayList<ResultModel> resModelList; 
ArrayList<Entry> yVals1; 

구문 분석 JSON은

//getResult 
public class JSONTask extends AsyncTask<String, String, List<ResultModel>> { 

    @Override 
    protected List<ResultModel> doInBackground(String... params) { 
     BufferedReader reader = null; 
     HttpURLConnection connection = null; 
     try { 
      URL url = new URL(params[0]); 
      connection = (HttpURLConnection) url.openConnection(); 
      connection.setRequestProperty("Cookie",session_name+"="+session_id); 
      connection.setRequestProperty("X-CSRF-Token", token); 
      //connection.setRequestProperty("Accept-Encoding", "identity"); 
      connection.connect(); 

      int length = connection.getContentLength(); 

      InputStream stream = connection.getInputStream(); 
      reader = new BufferedReader(new InputStreamReader(stream)); 

      String line = ""; 
      StringBuffer buffer = new StringBuffer(); 
      while ((line = reader.readLine()) != null){ 
       buffer.append(line); 
      } 

      Log.v("TESt", " " + length); 

      String finalJson = buffer.toString(); 
      JSONArray parentArray = new JSONArray(finalJson); 
      resModelList = new ArrayList<>(); 

      for(int i=0; i<parentArray.length(); i++){ 
       JSONObject finalObject = parentArray.getJSONObject(i); 
       ResultModel resModel = new ResultModel(); 
       resModel.setPost_date(finalObject.getString("post_date")); 
       resModel.setHow_much_has_ocd(finalObject.getString("how_much_has_ocd")); 
       resModelList.add(resModel); 
      } 
      return resModelList; 

     }catch (MalformedURLException e){ 
      e.printStackTrace(); 
     }catch (IOException e){ 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } finally { 
      if(connection != null) { 
       connection.disconnect(); 
      } 
      try { 
       if(reader != null){ 
        reader.close(); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(List<ResultModel> result) { 
     super.onPostExecute(result); 
     if(!resModelList.isEmpty()){ 
//here is where i get my errors 
      yVals1 = new ArrayList<Entry>(); 
      for (ResultModel ocd : resModelList){ 
       int score = Integer.parseInt(ocd.getHow_much_has_ocd()); 
       int timeStamp = Integer.parseInt(ocd.getPost_date()); 
//I get these log values 
       Log.v("Score: ", " " + score + " Timestamp: " + timeStamp); 
       yVals1.add(new Entry(timeStamp, score)); 
      } 
      graph(); 
      Log.v("Not Empty list", ""); 
     }else { 
      Log.v("Empty list", ""); 
     } 
    } 
} 

finalJson는 수도 그래서 초보자입니다

[{"post_date":"1481895820","did_you_avoid":"25","how_much_has_ocd":"81","how_would_you_rate":"82","overall_how_would":"35","were_there_any_distressing":"0","uid":"2"},{"post_date":"1481723564","did_you_avoid":"13","how_much_has_ocd":"10","how_would_you_rate":"13","overall_how_would":"16","were_there_any_distressing":"0","uid":"2"},{"post_date":"1481723488","did_you_avoid":"28","how_much_has_ocd":"56","how_would_you_rate":"75","overall_how_would":"32","were_there_any_distressing":"0","uid":"2"},{"post_date":"1481537274","did_you_avoid":"53","how_much_has_ocd":"59","how_would_you_rate":"15","overall_how_would":"71","were_there_any_distressing":"1","uid":"2"},{"post_date":"1481295470","did_you_avoid":"67","how_much_has_ocd":"64","how_would_you_rate":"66","overall_how_would":"57","were_there_any_distressing":"0","uid":"2"},{"post_date":"1481097609","did_you_avoid":"72","how_much_has_ocd":"85","how_would_you_rate":"62","overall_how_would":"64","were_there_any_distressing":"0","uid":"2"},{"post_date":"1480673252","did_you_avoid":"33","how_much_has_ocd":"69","how_would_you_rate":"84","overall_how_would":"37","were_there_any_distressing":"1","uid":"2"}, 

을 log.v 나타나서 단순한 실수는 아닐 것이다.
미리 감사드립니다.

+0

"finalJson"값을 게시 할 수 있습니까? 그리고 linenumber 예외가 – NehaK

+2

코드의 어느 라인에서 NegativeArraySizeException이 발생 했습니까? – Fartab

+0

finalJson 로그 아웃을 게시했습니다. 나는 모든 가치를 얻고있다. –

답변

0

오류가 발견되었습니다. 그것은 내 그래프 라이브러리였습니다.

저는 MPAndroidChart 라이브러리를 사용하고 있으며 데이터를 정렬해야합니다. 내 백엔드가 분류되었습니다. 게시 날짜 (오름차순)로 변경해야했습니다.

이 문제와 관련 있습니다. NegativeArraySizeException adding Scatter Data to a CombinedChart

다른 사람에게 도움이되기를 바랍니다.

+0

슬프게도 나는 같은 라이브러리를 사용하지 않는다. 좋은 성향을 가져 주셔서 감사합니다. – statosdotcom

관련 문제