2016-11-11 43 views
0

所以我试图将我的应用程序连接到亚马逊云Nosql服务器DynamoDB,我一直遵循他们的教程,但似乎无法得到它的工作,因为这个错误,保持发生:亚马逊网络服务DynamoDB错误com.amazonaws.auth.AWSCredentials

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

回答

1

bug是在CredentialsProviderSingleton

您创建一个crednetials提供商作为一个新的变量

CognitoCachingCredentialsProvider credentialsProvider =新CognitoCachingCredentialsProvider( context.getApplicationContext() ,“POOL_ID”,Regions.US_WEST_2);

但是你返回类变量credentialProvider(注意它在证书的末尾没有's')。这个变量是空的,因为它没有被声明过。

+0

因此,将CognitoCachingCredentialsProvider credentialsProvider更改为credentialProvider。 – jarmod