我正在尝试使用Amplify Auth将用户添加到Cognito用户池。 我写了这个简单的代码,但即使它也不起作用:
try {
Amplify.addPlugin(new AWSCognitoAuthPlugin());
} catch (AmplifyException e) {
e.printStackTrace();
}
Amplify.Auth.signUp(
"dragon",
"Password123@",
AuthSignUpOptions.builder()
.userAttribute(AuthUserAttributeKey.email(), "myemailaddress@gmail.com")
.build(),
result -> Log.i("AuthQuickStart", "Result: " + result.toString()),
error -> Log.e("AuthQuickStart", "Sign up failed", error)
);
它将此错误输出到logcat:
E/AuthQuickStart: Sign up failed
AmplifyException {message=Sign up failed, cause=java.lang.NullPointerException: Attempt to invoke virtual method 'void com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool.signUp(java.lang.String, java.lang.String, com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserAttributes, java.util.Map, com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.SignUpHandler)' on a null object reference, recoverySuggestion=See attached exception for more details}
at com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin$2.onError(AWSCognitoAuthPlugin.java:1)
at com.amazonaws.mobile.client.internal.InternalCallback.call(InternalCallback.java:77)
at com.amazonaws.mobile.client.internal.InternalCallback.access$000(InternalCallback.java:34)
at com.amazonaws.mobile.client.internal.InternalCallback$1.run(InternalCallback.java:103)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool.signUp(java.lang.String, java.lang.String, com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserAttributes, java.util.Map, com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.SignUpHandler)' on a null object reference
at com.amazonaws.mobile.client.AWSMobileClient$13.run(AWSMobileClient.java:1894)
at com.amazonaws.mobile.client.internal.InternalCallback$1.run(InternalCallback.java:101)
at java.lang.Thread.run(Thread.java:764)
答案 0 :(得分:2)
您必须致电Amplify.configure()
一次。在添加插件之后,以及执行任何其他Amplify命令之前,应调用它。
Amplify框架可能应该返回一条错误消息,指出该错误消息,而不是它现在正在做什么。这是框架错误报告中的错误。