皆さん、Android StudioのDynamoDBのテーブルからデータを読み込もうとしていますが、私の手間がかかりましたが、この例外が発生しています。どのようにそれを修正するための任意のアイデア?AWS DynamoDB RuntimeException
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;
}
}
アクセスしようとしているリソースのコマンドを受け入れるようにIAMロールを設定していることを確認してください。非常に一般的なことを人々が忘れる – iSkore