0

Hey guys im trying to read some data from a table in DynamoDB in Android Studio and despite my arduous endeavors, I keep getting this exception. Any ideas how to fix it?

04-06 19:25:11.688 23407-23423/com.example.ali.dynamodb E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.example.ali.dynamodb, PID: 23407
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'com.amazonaws.auth.AWSCredentials com.amazonaws.auth.AWSCredentialsProvider.getCredentials()' on a null object reference
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:3504)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.getItem(AmazonDynamoDBClient.java:929)
at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.load(DynamoDBMapper.java:393)
at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.load(DynamoDBMapper.java:466)
at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.load(DynamoDBMapper.java:340)
at com.example.ali.dynamodb.MainActivity$BackT.doInBackground(MainActivity.java:172)
at com.example.ali.dynamodb.MainActivity$BackT.doInBackground(MainActivity.java:161)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
at java.lang.Thread.run(Thread.java:818)

MainActivity.java:

public class MainActivity extends AppCompatActivity {
        private CognitoCachingCredentialsProvider credentialsProvider;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
                setSupportActionBar(toolbar);

                String forumName = "Amazon DynamoDB";
                String threadSubject = "DynamoDB Thread 1";

                FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
                fab.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                                                .setAction("Action", null).show();
                        }
                });

                // Initialize the Amazon Cognito credentials provider
                CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
                                getApplicationContext(),
                                "eu-west-1:c9e997aa-5f3e-4c0a-b3d9-f6faf2a39ab7", // Identity Pool ID
                                Regions.EU_WEST_1 // Region
                );


                new BackT().execute("");
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.menu_main, menu);
                return true;
        }

        private static void GetUserName(DynamoDBMapper mapper, int id) throws Exception {
                UserTable userTable = mapper.load(UserTable.class, id);
        }


        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
                // Handle action bar item clicks here. The action bar will
                // automatically handle clicks on the Home/Up button, so long
                // as you specify a parent activity in AndroidManifest.xml.
                int id = item.getItemId();

                //noinspection SimplifiableIfStatement
                if (id == R.id.action_settings) {
                        return true;
                }

                return super.onOptionsItemSelected(item);
        }


        private class BackT extends AsyncTask<String, Void, String> {
                @Override
                protected String doInBackground(String... params) {
                        AmazonDynamoDBClient ddbClient = new AmazonDynamoDBClient(credentialsProvider);

                        TextView sample1 = (TextView) findViewById(R.id.sample1);
                        TextView sample2 = (TextView) findViewById(R.id.sample2);
                        TextView sample3 = (TextView) findViewById(R.id.sample3);

                        // Getting items from the row specified with primary key
                        DynamoDBMapper mapper = new DynamoDBMapper(ddbClient);
                        UserTable userTable = mapper.load(UserTable.class, 1);
                        String result1 = userTable.getFirstName();
                        String result2 = userTable.getSurname();
                        String result3 = userTable.getUserName();

                        sample1.setText(result1);
                        sample2.setText(result2);
                        sample3.setText(result3);

                        mapper.save(userTable);
                        return "Executed";
                }

                @Override
                protected void onPostExecute(String result) {
                }

                @Override
                protected void onPreExecute() {
                }

                @Override
                protected void onProgressUpdate(Void... values) {
                }
        }
}

UserTable.java:

@DynamoDBTable(tableName = "USER_TABLE")
public class UserTable {
        private String dateOfBirth;
        private String emailAddress;
        private String FirstN;
        private String SurN;
        private String User_Acc_ID;
        private String User_Address_ID;
        private int User_ID;
        private String User_Name;

        @DynamoDBAttribute(attributeName = "DATE_OF_BIRTH")
        public String getDOB() {
                return dateOfBirth;
        }

        @DynamoDBAttribute(attributeName = "EMAIL_ADDRESS")
        public String getEmailAddress() {
                return emailAddress;
        }

        @DynamoDBAttribute(attributeName = "FIRST_NAME")
        public String getFirstName() {
                return FirstN;
        }

        @DynamoDBAttribute(attributeName = "SURNAME")
        public String getSurname() {
                return SurN;
        }

        @DynamoDBAttribute(attributeName = "USER_ACC_ID")
        public String getUser_Acc_ID() {
                return User_Acc_ID;
        }

        @DynamoDBAttribute(attributeName = "USER_ADDRESS_ID")
        public String getUser_Address_ID() {
                return User_Address_ID;
        }

        @DynamoDBHashKey(attributeName = "USER_ID")
        public int getUserID() {
                return User_ID;
        }

        public void setUserID(int user_id){
                this.User_ID = User_ID;
        }


        @DynamoDBAttribute(attributeName = "USER_ID")
        public String getUserName() {
                return User_Name;
        }

}
1
  • Make sure you have you IAM roles set up to accept the commands for the resource you're trying to access. Very common thing people forget Commented Apr 6, 2016 at 22:43

1 Answer 1

2

You create a second instance credentialsProvider which is hiding the class member. As result the used class member credentialsProvider is not initialized and null.

Remove CognitoCachingCredentialsProvider at the initialization line.

Edit: I'm not familar with android specials. But from a jav perspecptive it should loop like:

new BackT(credentialsProvider).execute("");

And the BackT class should contain a constructor:

private AWSCredentialsProvider localCredProvider = null;
public BackT(AWSCredentialsProvider credProvider)
{
   if (null != credProvider)
   {
     this.localCredProvider = credProvider;
   }
   else
   {
      // add some error logging here
   }
}

and the DB initialization should use the local member:

new AmazonDynamoDBClient(localCredProvider);

Something that way you should try. Otherwise the credential provider returns a NULL value somewhere.

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

3 Comments

Hey Konrad thank you for your answer. i must have missed this one out and i mended it. However i still get the same error - at com.example.ali.dynamodb.MainActivity$BackT.doInBackground(MainActivity.java:148) at com.example.ali.dynamodb.MainActivity$BackT.doInBackground(MainActivity.java:137) which is BackT class
Your line numbers does not match with the code snippets. Can you try to pass the credentials object to the BackT instance as constructor parameter?
Can you please give an example (sorry, im new)

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.