0

so I am trying to connect my app to amazons cloud Nosql server DynamoDB, I have been following thier tutorial but can not seem to get it work because of this error that keeps happening:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.humber.industech.industechapp/com.humber.industech.industechapp.DataActivity}: 
java.lang.NullPointerException: Attempt to invoke interface method 'com.amazonaws.auth.AWSCredentials com.amazonaws.auth.AWSCredentialsProvider.getCredentials()' on a null object reference

Any help on getting setup using AWS would be greatly appreciated, Thanks a lot!

Code is below:

public class DataActivity extends AppCompatActivity {

    private TextView t;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data);
        //setting custom font
        t = (TextView) findViewById(R.id.textView3);
        Typeface customFont = Typeface.createFromAsset(getAssets(),"fonts/Prezident.ttf");
        t.setTypeface(customFont);
        saveData();
    }

     public void saveData(){
        CognitoCachingCredentialsProvider credentialsProvider = CredentialProviderSingleton.getInstance(this);
        AmazonDynamoDBClient ddbClient = new AmazonDynamoDBClient(credentialsProvider);
        DynamoDBMapper mapper = new DynamoDBMapper(ddbClient);
        Book book = new Book();
        book.setTitle("Test");
        book.setAuthor("Charles Dickens");
        book.setPrice(1299);
        book.setIsbn("1235674");
        book.setHardCover(false);
        mapper.save(book);
    }
}



public class CredentialProviderSingleton {

    static CognitoCachingCredentialsProvider credentialProvider;

    public static CognitoCachingCredentialsProvider getInstance(Context context){
        if (credentialProvider == null){
            CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
                    context.getApplicationContext(),"POOL_ID", Regions.US_WEST_2);
        }
        return credentialProvider;
    }
}

1 Answer 1

1

The bug is in the CredentialsProviderSingleton

You create a crednetials provider as a new variable

CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider( context.getApplicationContext(),"POOL_ID", Regions.US_WEST_2);

But you return the class variable credentialProvider (note it has no 's' at the end of credentials). This variable is null because it has nnever been declared.

Sign up to request clarification or add additional context in comments.

1 Comment

So change CognitoCachingCredentialsProvider credentialsProvider to credentialProvider.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.