checking if facebook-sdk android giving null value for user data before calling getProperty()

微笑、不失礼 提交于 2019-12-24 21:15:25

问题


I searched I can't find any solution for this problem. I have made graph-request in facebook SDK 3.5. When I try to get data using getProperty("Property_name") and if its value is null maybe user didn't set the favourite sports in his profile

Example: Code:For buildUserInfoDisplay(GraphUser user) Method

    // I have used all The mandatory permissions

    Map<String, String> map;

    map = new HashMap<String, String>();

    try {

        map.put("ID", user.getId().toString());

        String athletes = user.getProperty("favorite_athletes").toString();
        String fav_teams = user.getProperty("favorite_teams").toString();
        String sports = user.getProperty("sports").toString();
        String education = user.getProperty("education").toString();
        String work = user.getProperty("work").toString();

        map.put("athletes", athletes);
        map.put("fav teams", fav_teams);
        map.put("sports", sports);
        map.put("education", education);
        map.put("work", work);
        }
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
        Log.e(TAG + " Error:", e.toString());
    }

    //PassData is Interface object send data in Map<Strin String> format to Activity launching this Fragment
    try {
        passData(map); //Passing data through Interface
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.e(TAG + " Error 2:", e.toString());         
    }
}`

Error
StackTrace : 

`java.lang.NullPointerException
at com.main.hotspot.MainFragment.buildUserInfoDisplay(MainFragment.java:177)
at com.main.hotspot.MainFragment.access$1(MainFragment.java:162)
at com.main.hotspot.MainFragment$2.onCompleted(MainFragment.java:77)
at com.facebook.Request$1.onCompleted(Request.java:269)
at com.facebook.Request$4.run(Request.java:1669)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4340)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)

MainFragment Error:(646): java.lang.NullPointerException`

line 177 is where I call for sports data:

String sports = user.getProperty("sports").toString(); // line 177

if sports property is not present in response so getProperty() will give NullPointerException. and further I tried to pass data in Map format from fragment to Activity launching this fragment by defining Interface. But because of Exception I can't pass data to activity. I want to process it in Activity.

how can I check before calling the getProperty() that this value is present in user object.

Thanks.


回答1:


The issue is that you're calling .toString on a potentially null object, and is not really related to the SDK at all. Common java idiom is to protect your calls with null checks. Something like:

Object athletes = user.getProperty("favorite_athletes");
if (athletes != null) {
  map.put("athletes", athletes.toString());
}
...


来源:https://stackoverflow.com/questions/20534834/checking-if-facebook-sdk-android-giving-null-value-for-user-data-before-calling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!