0

I'm attempting create my first dynamodb app on android and get receiving a null-pointer error that I can not figure out why.

I've written the following asyntask:

class CheckIfEmailNull extends AsyncTask<String, Void, Void>{

        DynamoDBMapper dynamoDBMapper;


        @Override
        protected Void doInBackground(String... userID) {

            AmazonDynamoDBClient dynamoDBClient = new AmazonDynamoDBClient(AWSMobileClient.getInstance().getCredentialsProvider());
            this.dynamoDBMapper = DynamoDBMapper.builder()
                    .dynamoDBClient(dynamoDBClient)
                    .awsConfiguration(AWSMobileClient.getInstance().getConfiguration())
                    .build();

            userB = dynamoDBMapper.load(SecureUsersDO.class, userID[0]);
            return null;
        }
    }

With the following method to written to initiate it:

public void checkUserEmail(Profile profileTest){

        Log.d("checkUserEmail>>>", profileTest.getId().toString());

        CheckIfEmailNull checkIfEmailNull = new CheckIfEmailNull();
        checkIfEmailNull.execute(profileTest.getId().toString());

        if (userB.getUserId() == profileTest.getCurrentProfile().getId()){
            Log.d("ID's match: ", "attempting");
            if (userB.getEmail() == null){
                Log.d("TODO", "Write code to show we need email");
                Log.d("User B is : ", userB.getFirstName());
            }
        }
    }

Yet i get the following error:

05-19 09:33:37.034 6156-6156/com.ronone.securesender E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.ronone.securesender, PID: 6156
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ronone.securesender/com.ronone.securesender.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.ronone.securesender.amazonaws.models.nosql.SecureUsersDO.getUserId()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.ronone.securesender.amazonaws.models.nosql.SecureUsersDO.getUserId()' on a null object reference
        at com.ronone.securesender.HomeActivity.checkUserEmail(HomeActivity.java:180)
        at com.ronone.securesender.HomeActivity.onCreate(HomeActivity.java:66)
        at android.app.Activity.performCreate(Activity.java:7009)
        at android.app.Activity.performCreate(Activity.java:7000)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)

Thanks for any help in advance.

  • You can't start an async task and immediately expect `userB` to have a value. Do that from either `doInBackground` or more likely from [`onPostExecute`](https://developer.android.com/reference/android/os/AsyncTask#usage) – zapl May 19 '18 at 14:13
  • Have you download the models from AWS Mobile Hub and added to your app? – Karthikeyan May 21 '18 at 16:01
  • It is possible that `checkUserEmail` is getting executed before `CheckIfEmailNull` is able to load the models class and instantiate userB object. Hence if userB object is null, you would get this crash. – Karthikeyan May 23 '18 at 17:19

0 Answers0