当我尝试从我的dynamoDB表中查询数据时,我得到一个空对象引用错误:
mapper = new DynamoDBMapper(ddbClient);
DataMapperClass dataMapperClass = new DataMapperClass();
dataMapperClass.setHash("theHashValueIset");
String queryString = String.valueOf("theRangeValueIset");
Condition rangeKeyCondition = new Condition()
.withComparisonOperator(ComparisonOperator.BEGINS_WITH.toString())
.withAttributeValueList(new AttributeValue().withS(queryString.toString()));
DynamoDBQueryExpression <DataMapperClass> queryExpression = new DynamoDBQueryExpression<DataMapperClass>()
.withHashKeyValues(dataMapperClass)
.withRangeKeyCondition("rangeAttributeOnTable", rangeKeyCondition)
.withConsistentRead(false);
PaginatedQueryList<DataMapperClass> result = mapper.query(DataMapperClass.class, queryExpression);
我的DataMapperClass.class:
@DynamoDBTable(tableName = "myTableName")
public class DataMapperClass {
private String hash;//based on a set of categories I chose
private String objectID; //auto generated
//Hash
@DynamoDBHashKey(attributeName = "hashNameOnMyTable")
public String getHash() { return hash; }
public void setHash(String hash) { this.hash = hash;}
//ObjectID
@DynamoDBRangeKey(attributeName = "rangeAttributeOnTable")
@DynamoDBAutoGeneratedKey
public String getObjectID() {return objectID;}
public void setObjectID(String objectID) { this.objectID = objectID; }
/* other @DynamoDBAttribute*/
}
我如何解决这个错误,我得到了#34; PaginatedQueryList&#34;代码行:
Attempt to invoke interface method 'com.amazonaws.services.dynamodbv2.model.QueryResult com.amazonaws.services.dynamodbv2.AmazonDynamoDB.query(com.amazonaws.services.dynamodbv2.model.QueryRequest)' on a null object reference
答案 0 :(得分:0)
问题解决了!
我意识到我没有在onCreate()中执行此操作:
ddbClient = new AmazonDynamoDBClient(identityManager.getCredentialsProvider());