Java 如何使用try-and-catch从方法正确返回字符串

Java 如何使用try-and-catch从方法正确返回字符串,java,android,facebook,parsing,Java,Android,Facebook,Parsing,每次调用此方法时,它总是返回null,即使userProfile.getfacebookId的值不是null public String updateProfilePicture() { String facebookId = null; if (currentUser.get("profile") != null) { JSONObject userProfile = currentUser.getJSONObject("profile");

每次调用此方法时,它总是返回null,即使userProfile.getfacebookId的值不是null

public String updateProfilePicture() {

    String facebookId = null;

    if (currentUser.get("profile") != null) {
        JSONObject userProfile = currentUser.getJSONObject("profile");
        try {
            if (userProfile.getString("facebookId") != null) {

                facebookId = userProfile.get("facebookId").toString();

            } else {

                facebookId = null;

            }

        } catch (JSONException e) {
            // handle error
            facebookId = null;
        }
    }


    return facebookId;
}

把它记录下来然后贴上日志你确定吗?尝试调试应用程序&从返回的角度来看facebookIdlooks good中的值。你确定json包含那个键吗?我看也没问题。。。但为什么要混合使用userProfile.getStringfacebookId和userProfile.getfacebookId.toString?但乍一看,这也不应该是个问题。。。我们需要更多的信息来研究它。在catch块中记录异常以确保没有抛出JSONException。