2012-12-06 3 views
1

다음 오류가 발생했습니다. java.lang.NullPointerException : lock == null 서버에서 json 파일을 가져 오는 동안. google하지만 위의 모든 솔루션을 찾을 수 없습니다.java.lang.NullPointerException : 서버에서 json 파일을 가져 오는 동안 잠금 == null

내 코드 :

try { 
      // defaultHttpClient 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 

      Log.i("clock", httpPost.getURI().toString()); 
      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent();   

     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     try { 
      Log.i("Buffer", "1"); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); 
      Log.i("Buffer", "2"); 
      StringBuilder sb = new StringBuilder(); 
      Log.i("Buffer", "3"); 
      String line = null; 
      Log.i("Buffer", "4"); 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      Log.i("Buffer", "5"); 
      json = sb.toString(); 
      Log.i("Buffer", "6->"+json); 
     } catch (Exception e) { 
      Log.e("Buffer Error", "Error converting result-> " + e.toString()); 
     } 

로그 캣 :

enter image description here

+0

날 위해 일했습니다. – assylias

+0

전체 logcat 게시물을 게시하십시오. AndroidManifest.xml 파일에 인터넷 권한을 추가했는지 확인하십시오. – rajpara

답변

2

당신은 항상 언제 사용하기 전에 instance/ java objectnot null입니다 확인해야합니다.

예 : 내가 서버와 통신 할

// this method takes URL as input and returns JSON data 
//parent class name is "NewWebHelper.java" 

public String getResult(String url) { 

     Log.v(TAG, "Final Requsting URL is : :"+url); 

     String line = ""; 
     String responseJsonData = null; 

     try { 
      StringBuilder sb = new StringBuilder(); 
      String x = ""; 
      URL httpurl = new URL(url); 
      URLConnection tc= httpurl.openConnection(); 

      BufferedReader in = new BufferedReader(
           new InputStreamReader(tc.getInputStream())); 

      if(in !=null){ 
       while ((line = in.readLine()) != null) { 
       sb.append(line + "\n"); 
       x = sb.toString(); 
      } 
      responseJsonData = new String(x); 

      } 
     } 
     catch (UnknownHostException uh){    
      Log.v("NewWebHelper", "Unknown host :"); 
      uh.printStackTrace(); 
     } 
     catch (FileNotFoundException e) { 
      Log.v("NewWebHelper", "FileNotFoundException :"); 
      e.printStackTrace(); 
     } 
     catch (IOException e) { 
      Log.v("NewWebHelper", "IOException :"); 
      e.printStackTrace(); 
     } 
     catch (Exception e) { 
      Log.v("NewWebHelper", "Exception :"); 
      e.printStackTrace(); 
     } 
     return responseJsonData; 
    } 

당신이 어떤 문제 regrading이 있으면 알려주세요 경우

if(httpResponse != null){ 
    //todo your work here with "httpResponse " 
} 

하고 여기에

if(is !=null){ 
    //todo your work here with "is" 
} 

은 코드 예제입니다 이. 예를 들어

2

:

 JSONObject jRoot = new JSONObject(json); 
     if (!jRoot.isNull("response")) { 

      JSONObject jChat = jRoot.getJSONObject("response"); 

      if (!jChat.isNull("type")) { 
       ch.setType(jChat.getString("type")); 
      } 

사용 기능 isNull 또는 사용 조건 당신은 단지 매니페스트에서의 인터넷 액세스 권한을 부여 할 필요가 if (is != null)

0

. 당신은, 당신은 문제가 무엇인지 줄 알 것 대신 예외의 메시지의 전체 스택 트레이스를 기록하는 경우

<uses-permission android:name="android.permission.INTERNET"></uses-permission> 

은 ...

관련 문제