gpt4 book ai didi

java - Amazon Web Service DynamoDB 错误 com.amazonaws.auth.AWSCredentials

转载 作者:行者123 更新时间:2023-12-01 09:15:48 26 4
gpt4 key购买 nike

所以我尝试将我的应用程序连接到亚马逊云 Nosql 服务器 DynamoDB,我一直在遵循他们的教程,但由于不断发生此错误,似乎无法使其工作:

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

任何有关使用 AWS 进行设置的帮助将不胜感激,非常感谢!

代码如下:

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;
}
}

最佳答案

该错误位于 CredentialsProviderSingleton 中

您创建一个凭证提供程序作为新变量

CognitoCachingCredentialsProvider凭据提供程序=新CognitoCachingCredentialsProvider( context.getApplicationContext(),"POOL_ID", Regions.US_WEST_2);

但是您返回类变量 credentialProvider (注意它在凭据末尾没有“s”)。该变量为 null,因为它从未被声明过。

关于java - Amazon Web Service DynamoDB 错误 com.amazonaws.auth.AWSCredentials,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40555834/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com