Source:
<code>private void initializeLexSDK() { Log.d(TAG, "Lex Client"); final CountDownLatch initializeLatch = new CountDownLatch(1); final CountDownLatch getCredentialsLatch = new CountDownLatch(1); AWSMobileClient.getInstance().initialize(this, new Callback<UserStateDetails>() { @Override public void onResult(UserStateDetails result) { Log.d(TAG, "initialize.onResult, userState: " + result.getUserState().toString()); initializeLatch.countDown(); } @Override public void onError(Exception e) { Log.e(TAG, "initialize.onError: ", e); initializeLatch.countDown(); } }); try { initializeLatch.await(); <START> AWSMobileClient.getInstance().getAWSCredentials(new Callback<AWSCredentials>() { <END> @Override public void onResult(AWSCredentials result) { Log.d(TAG, "getAWSCredentials.onResult, credentials: " + result.toString()); getCredentialsLatch.countDown(); } @Override public void onError(Exception e) { Log.e(TAG, "getAWSCredentials.onError: ", e); getCredentialsLatch.countDown(); } }); getCredentialsLatch.await(); String identityId = AWSMobileClient.getInstance().getIdentityId(); Log.d(TAG, "identityId: " + identityId); InteractionConfig lexInteractionConfig = new InteractionConfig( appContext.getResources().getString(R.string.bot_name), appContext.getResources().getString(R.string.bot_alias), identityId); lexInteractionClient = new InteractionClient(getApplicationContext(), AWSMobileClient.getInstance(), Regions.fromName(appContext.getResources().getString(R.string.lex_region)), lexInteractionConfig); lexInteractionClient.setAudioPlaybackListener(audioPlaybackListener); lexInteractionClient.setInteractionListener(interactionListener); } catch (InterruptedException e) { e.printStackTrace(); } }</code><technical_language>In case of AWSMobileClient initialization error, counting latch getAWSCredentials call result in NPE. consider adding a check for success initialization of AWSMobileClient</technical_language>

Target:
private void initializeLexSDK() { Log.d(TAG, "Lex Client"); final CountDownLatch initializeLatch = new CountDownLatch(1); final CountDownLatch getCredentialsLatch = new CountDownLatch(1); AWSMobileClient.getInstance().initialize(this, new Callback<UserStateDetails>() { @Override public void onResult(UserStateDetails result) { Log.d(TAG, "initialize.onResult, userState: " + result.getUserState().toString()); initializeLatch.countDown(); } @Override public void onError(Exception e) { Log.e(TAG, "initialize.onError: ", e); initializeLatch.countDown(); } }); try { initializeLatch.await(); AWSMobileClient.getInstance().getAWSCredentials(new Callback<AWSCredentials>() { @Override public void onResult(AWSCredentials result) { Log.d(TAG, "obtained AWS credentials successfully."); getCredentialsLatch.countDown(); } @Override public void onError(Exception e) { Log.e(TAG, "getAWSCredentials.onError: ", e); getCredentialsLatch.countDown(); } }); getCredentialsLatch.await(); String identityId = AWSMobileClient.getInstance().getIdentityId(); Log.d(TAG, "identityId: " + identityId); InteractionConfig lexInteractionConfig = new InteractionConfig( appContext.getResources().getString(R.string.bot_name), appContext.getResources().getString(R.string.bot_alias), identityId); lexInteractionClient = new InteractionClient(getApplicationContext(), AWSMobileClient.getInstance(), Regions.fromName(appContext.getResources().getString(R.string.lex_region)), lexInteractionConfig); lexInteractionClient.setAudioPlaybackListener(audioPlaybackListener); lexInteractionClient.setInteractionListener(interactionListener); } catch (InterruptedException e) { e.printStackTrace(); } }