2012-06-16 5 views
1

아래 코드에서 JSONObject json = userFunctions.uploadFileData(guid, photoName);의 NullPointerException 오류가 계속 발생합니다. 왜이 오류가 발생하는지 잘 모르겠습니다. 이 문제를 어떻게 해결할 수 있는지 알려주세요. 여기 json 객체에 대해 NullPointerException 받기

여기 내 코드

입니다이 오류가 내 JSONParser 클래스를 여기

public boolean fileData(String guid, String photoName) { 
    Toast.makeText(getApplicationContext(), photoName, Toast.LENGTH_LONG).show(); 

    JSONObject json = userFunctions.uploadFileData(guid, photoName); 

    try { 
     if(Integer.parseInt(json.getString("success")) != 1) { 
      Toast.makeText(getApplicationContext(), json.getInt("error_msg"), Toast.LENGTH_LONG).show(); 
      //register_error.setText(json.getString("error_msg")); 
      return false; 
     } 
    } catch (NumberFormatException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return true; 
} 

발생 내 uploadedFileData 방법

public JSONObject uploadFileData(String uid, String photoName) { 
    List<NameValuePair> params = new ArrayList<NameValuePair>(); 
    params.add(new BasicNameValuePair("tag", file_tag)); 
    params.add(new BasicNameValuePair("uid", uid)); 
    params.add(new BasicNameValuePair("photoName", photoName)); 
    JSONObject json = jsonParser.getJSONFromUrl(url, params); 
    Log.e("Some",json.toString()); 
    return json; 
} 

이며, 여기있는 방법이다

public class JSONParser { 
static InputStream is = null; 
static JSONObject jObj = null; 
static String json = ""; 

public JSONParser() { 

} 

public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) { 

    try { 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 
     httpPost.setEntity(new UrlEncodedFormEntity(params)); 

     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 { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 

     while((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 

     is.close(); 
     json = sb.toString(); 
     //Log.e("JSON", json); 
    } catch (Exception e) { 
     //e.printStackTrace(); 
     Log.e("Buffer Error", "Error converting result " + e.toString()); 
    } 

    // try parse the string to a JSON object 
    try { 
     jObj = new JSONObject(json); 
    } catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
    } 

    return jObj; 
} 
} 

가 여기에 예외 스택 추적 userFunctions에서 나의 로그 캣

06-16 17:03:47.890: E/AndroidRuntime(12364): FATAL EXCEPTION: main 
06-16 17:03:47.890: E/AndroidRuntime(12364): java.lang.NullPointerException 
06-16 17:03:47.890: E/AndroidRuntime(12364): at com.zafar.login.Camera.fileData(Camera.java:216) 
06-16 17:03:47.890: E/AndroidRuntime(12364): at com.zafar.login.Camera$2.run(Camera.java:186) 
06-16 17:03:47.890: E/AndroidRuntime(12364): at android.os.Handler.handleCallback(Handler.java:587) 
06-16 17:03:47.890: E/AndroidRuntime(12364): at android.os.Handler.dispatchMessage(Handler.java:92) 
06-16 17:03:47.890: E/AndroidRuntime(12364): at android.os.Looper.loop(Looper.java:130) 
06-16 17:03:47.890: E/AndroidRuntime(12364): at android.app.ActivityThread.main(ActivityThread.java:3691) 
06-16 17:03:47.890: E/AndroidRuntime(12364): at java.lang.reflect.Method.invokeNative(Native Method) 
06-16 17:03:47.890: E/AndroidRuntime(12364): at java.lang.reflect.Method.invoke(Method.java:507) 
06-16 17:03:47.890: E/AndroidRuntime(12364): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907) 
06-16 17:03:47.890: E/AndroidRuntime(12364): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665) 
06-16 17:03:47.890: E/AndroidRuntime(12364): at dalvik.system.NativeStart.main(Native Method) 
06-16 17:03:55.115: I/Process(12364): Sending signal. PID: 12364 SIG: 9 
+0

userFunctions를 초기화하고 fileData를 호출 한 곳 (실행중인 것으로 보입니다)은 어디입니까? 그 코드도 함께 제공하십시오. –

답변

1

당신이 json.toString을 사용하고 있기 때문에 JSON이 이유가 될 수 없기 때문에 null도 가능 코드 userFunctions 당 있도록 (응용 프로그램에서) 마지막 함수는()이다 uploadFileData에 있지만 트레이스가 ..... .....

+0

답을 얻지 못했기 때문에 답을 더 자세히 설명해 주시겠습니까? – 2619

+0

가능한 변수 null 인 스택 추적을 기반으로합니다. userFunctions try if (null == userFunctions) {Toast (....). show(); return;} JSONObject 앞에 json = userFunctions.uploadFileData (guid, photoName) –

+0

그래, 알았다. 오류는 userFunctions를 UserFunctions 클래스의 객체로 만들지 않았기 때문입니다. 이제는 효과가 있습니다. – 2619