2012-06-16 72 views
1

我在下面的代碼中不斷收到NullPointerException錯誤,在行JSONObject json = userFunctions.uploadFileData(guid, photoName);。我不知道爲什麼我得到這個錯誤。請幫我解決這個問題。爲json對象獲取NullPointerException

這裏是我的代碼

這裏是發生這種錯誤

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; 
} 

,這裏是我的JSONParser類中的方法

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; 
} 
} 

這是我的logcat

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

在異常堆棧跟蹤userFunctions是最後一個函數(在你的應用程序),從而每代碼userFunctions可以爲空,因爲,因爲你正在使用json.toString JSON不能的原因()在uploadFileData中,但跟蹤不是直到那裏.....

+0

您能否詳細說明您的答案,因爲我沒有得到您的答案。 – 2619

+0

它基於堆棧跟蹤,可能的變量null是userFunctions,請嘗試if(null == userFunctions){Toast(....)。show();返回;} JSONObject之前json = userFunctions.uploadFileData(guid,photoName) –

+0

是的,我知道了。錯誤是我沒有創建userFunctions作爲UserFunctions類的對象。現在它正在工作。 – 2619