Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Timothy Boland
Timothy Boland
18,237 Points

Receiving SharedPreferences.getString(java.lang.String, java.lang.String)' on a null object reference

I get the following error when running the app:

07-10 17:33:06.208 1474-1474/com.racecondtion.android.retrofitworkshop E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                         Process: com.racecondtion.android.retrofitworkshop, PID: 1474
                                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.racecondtion.android.retrofitworkshop/com.racecondtion.android.retrofitworkshop.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String android.content.SharedPreferences.getString(java.lang.String, java.lang.String)' on a null object reference
                                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                             at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                             at android.os.Looper.loop(Looper.java:148)
                                                                                             at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                          Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String android.content.SharedPreferences.getString(java.lang.String, java.lang.String)' on a null object reference
                                                                                             at com.racecondtion.android.retrofitworkshop.api.OAuthUtil.get(OAuthUtil.java:40)
                                                                                             at com.racecondtion.android.retrofitworkshop.api.OAuthUtil.isAuthorized(OAuthUtil.java:56)
                                                                                             at com.racecondtion.android.retrofitworkshop.MainActivity.onCreate(MainActivity.java:61)
                                                                                             at android.app.Activity.performCreate(Activity.java:6251)
                                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                             at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                             at android.os.Looper.loop(Looper.java:148) 
                                                                                             at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

Seems like its not getting any data from OAuthUtil.isAuthorized I think the lesson is missing some code. I noticed the code that sets OAuthUtil is in onResume, not onCreate or onStart, so maybe that is why the values are null. Did anyone else have this problem?

1 Answer

Timothy Boland
Timothy Boland
18,237 Points

This question was for the "Simplify Networking with RetroFit" android course. I was getting this error because i didnt have my BaseApplication class in the Android Manifest...Everything is good now