简体   繁体   English

空指针异常:尝试从Facebook获取用户位置时

[英]Null Pointer Exception: when trying to get user location from Facebook

I am trying to get user's location from Facebook, but getting Null Pointer Exception (NPE) and log looks like this: 我正在尝试从Facebook获取user's location ,但获取Null Pointer Exception (NPE)和log如下所示:

D/dalvikvm(3116): GC_FOR_ALLOC freed 413K, 15% free 2863K/3336K, paused 32ms, total 32ms
W/System.err(3116): java.lang.NullPointerException
W/System.err(3116):     at com.example.loginfb.MainActivity$2.onUserInfoFetched(MainActivity.java:50)
W/System.err(3116):     at com.facebook.widget.LoginButton$2.onCompleted(LoginButton.java:770)
W/System.err(3116):     at com.facebook.Request$1.onCompleted(Request.java:280)
W/System.err(3116):     at com.facebook.Request$4.run(Request.java:1658)
W/System.err(3116):     at android.os.Handler.handleCallback(Handler.java:730)
W/System.err(3116):     at android.os.Handler.dispatchMessage(Handler.java:92)
W/System.err(3116):     at android.os.Looper.loop(Looper.java:137)
W/System.err(3116):     at android.app.ActivityThread.main(ActivityThread.java:5103)
W/System.err(3116):     at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err(3116):     at java.lang.reflect.Method.invoke(Method.java:525)
W/System.err(3116):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
W/System.err(3116):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
W/System.err(3116):     at dalvik.system.NativeStart.main(Native Method)

MainActivity.java:- MainActivity.java:-

public void onUserInfoFetched(GraphUser user) {
                try {
                if (user != null) {
                    String strName = user.getFirstName();
                    String strLocation = user.getLocation().toString();

                    textUserName.setText("Welcome, " + user.getName()) ;
                    textUserInformation.setText
                    (
                            "Name: " + strName + "\n"
                            +
                            "Location: " + strLocation
                    );

                   } else {
                       textUserName.setText("You are not Logged In");
                       textUserInformation.setText("");

                   }
                }
                    catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
        });

I guess i am facing this issue, because has not given Location details on Facebook , but i want to show "Address not mentioned" in case user not provided to Facebook , along with other details. 我想我正面临这个问题,因为not given Location details on Facebook ,但是如果用户not provided to Facebook ,我想显示“地址未提及”以及其他详细信息。

Divide String strLocation = user.getLocation().toString(); 分隔String strLocation = user.getLocation().toString(); in this: 在此:

Object userLocation = user.getLocation();
String strLocation = (userLocation != null)? userLocation.toString() : "Address not mentioned";

I don't know the type of the Location object but if user.getLocation() returns null , you will have a java.lang.NullPointerException with your code. 我不知道Location对象的类型,但是如果user.getLocation()返回null ,则您的代码将带有java.lang.NullPointerException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实际上我正在开发一个天气预报应用程序,当我试图获取用户最后一个已知位置时,我得到了一个 null 指针异常 - Actually I'm working on a weather forecast app and I'm getting a null pointer exception when I'm trying to get the user last known location 尝试从文件中替换字符时出现空指针异常 - Null Pointer Exception, when trying to replace characters from file 尝试从Swing中的JTable检索数据时,出现空指针异常 - Null Pointer Exception when trying to retrieve data from JTable in Swing 尝试从另一个类调用方法时出现空指针异常? - Null pointer exception when trying to invoke the method from another class? 尝试设置值时出现空指针异常 - Null Pointer Exception when trying to set a value 尝试使用CommandExecutor时出现空指针异常 - Null Pointer Exception when trying to use a CommandExecutor 尝试访问ActionBar时出现空指针异常 - Null Pointer Exception When Trying To Access ActionBar 尝试比较数组时出现空指针异常 - Null Pointer Exception When Trying to Compare Arrays 尝试调用 getLayoutInflater 时出现空指针异常 - Null Pointer exception when trying to call getLayoutInflater 尝试访问类时出现空指针异常 - null pointer exception when trying to access class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM