Reputation: 657
I'm new to Parse.com, and am currently building my first project using Parse in Android. I am trying to replicate the login workflow shown in the 'Anywall' tutorial (https://www.parse.com/tutorials/anywall-android), but I am absolutely not getting it to work at all.
Whenever I try to launch the app, it gives me a NullPointerException where I am trying to check for (ParseUser.getCurrentUser() != null) in the DispatchActivity (code shown below):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Check if there is current user info
if (ParseUser.getCurrentUser() != null) {
// Start an intent for the logged in activity
startActivity(new Intent(this, MainActivity.class));
} else {
// Start and intent for the logged out activity
startActivity(new Intent(this, WelcomeActivity.class));
}
}
The stacktrace in LogCat is as under:
07-13 00:34:54.925 6268-6268/com.appzylabs.pratik.groupreceipts W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa6142908)
07-13 00:34:54.925 6268-6268/com.appzylabs.pratik.groupreceipts E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appzylabs.pratik.groupreceipts/com.appzylabs.pratik.groupreceipts.DispatchActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
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:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.parse.Parse.getParseDir(Parse.java:304)
at com.parse.ParseObject.getFromDisk(ParseObject.java:667)
at com.parse.ParseUser$10.then(ParseUser.java:1056)
at com.parse.ParseUser$10.then(ParseUser.java:1003)
at bolts.Task$14.run(Task.java:796)
at bolts.BoltsExecutors$ImmediateExecutor.execute(BoltsExecutors.java:105)
at bolts.Task.completeAfterTask(Task.java:787)
at bolts.Task.continueWithTask(Task.java:599)
at bolts.Task.continueWithTask(Task.java:610)
at com.parse.ParseUser.getCurrentUserAsync(ParseUser.java:1003)
at com.parse.ParseUser.access$800(ParseUser.java:26)
at com.parse.ParseUser$9.then(ParseUser.java:996)
at com.parse.ParseUser$9.then(ParseUser.java:993)
at com.parse.TaskQueue.enqueue(TaskQueue.java:61)
at com.parse.ParseUser.getCurrentUserAsync(ParseUser.java:993)
at com.parse.ParseUser.getCurrentUser(ParseUser.java:979)
at com.parse.ParseUser.getCurrentUser(ParseUser.java:966)
at com.appzylabs.pratik.groupreceipts.DispatchActivity.onCreate(DispatchActivity.java:26)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
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:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Something tells me that 'ParseUser' is not getting initialized or instantiated properly, but I already have the following code in my Application class:
public Application() {
}
@Override
public void onCreate() {
super.onCreate();
Parse.enableLocalDatastore(this);
Parse.initialize(this, "XXXX", "YYYY");
}
}
What am I doing wrong here? I just can't figure it out. Any help?
Upvotes: 2
Views: 1366
Reputation: 657
Well, I just realized the problem. I was calling Parse.initialize() in a separate Application class like so:
public class Application extends android.app.Application {
...
Parse.initialize(this, "XXXX", "YYYY");
...
}
However, I hadn't added the class to my AndroidManifest.xml so the code was never getting executed, Parse was never getting initialized, and ParseUser was throwing a NullPointerException. Once I added that to my manifest like under, it worked:
<application
android:name="com.appzylabs.pratik.groupreceipts.Application"
Sorry about that! I hope this is useful to others facing the same issue.
Upvotes: 2
Reputation: 2430
You need either to create new ParseUser object and call method ParseUser.signUp()
or to create new ParseUser with some credentials and call ParseUser.logIn()
to be able to use ParseUser.getCurrentUser()
as long as
This retrieves the currently logged in ParseUser with a valid session, either from memory or disk if necessary.
Upvotes: 0