我正在尝试将电话号码作为字符串从 Dynamodb 加载,但它显示 NullPointer 错误
这是我试图记录数据的我的活动代码
public class TokenActivity extends Activity {
DynamoDBMapper dynamoDBMapper;
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_token);
AWSMobileClient.getInstance().initialize(this).execute();
AWSCredentialsProvider credentialsProvider = AWSMobileClient.getInstance().getCredentialsProvider();
AWSConfiguration configuration = AWSMobileClient.getInstance().getConfiguration();
AmazonDynamoDBClient dynamoDBClient = new AmazonDynamoDBClient(credentialsProvider);
this.dynamoDBMapper = DynamoDBMapper.builder()
.dynamoDBClient(dynamoDBClient)
.awsConfiguration(configuration)
.build();
readName();
}
public void readName() {
new Thread(new Runnable() {
@Override
public void run() {
LoginCredentialsDO newsItem = dynamoDBMapper.load(
LoginCredentialsDO.class,
"userId",
"Phone Number");
// Item read
Log.d("News Item:", newsItem.getPhoneNumber());
}
}).start();
}
和 LoginCredentialsDO 包含从数据库中检索数据的方法。
块引用
public class LoginCredentialsDO {
private String _userId;
private String _phoneNumber;
private String _emailId;
private String _name;
@DynamoDBHashKey(attributeName = "userId")
@DynamoDBAttribute(attributeName = "userId")
public String getUserId() {
return _userId;
}
public void setUserId(final String _userId) {
this._userId = _userId;
}
@DynamoDBRangeKey(attributeName = "Phone Number")
@DynamoDBAttribute(attributeName = "Phone Number")
public String getPhoneNumber() {
return _phoneNumber;
}
public void setPhoneNumber(final String _phoneNumber) {
this._phoneNumber = _phoneNumber;
}
@DynamoDBAttribute(attributeName = "Email Id")
public String getEmailId() {
return _emailId;
}
public void setEmailId(final String _emailId) {
this._emailId = _emailId;
}
@DynamoDBAttribute(attributeName = "Name")
public String getName() {
return _name;
}
public void setName(final String _name) {
this._name = _name;
}
}
我收到 NullPointer 异常错误
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.....LoginCredentialsDO.getPhoneNumber()' on a null object reference
at com......TokenActivity$4.run(TokenActivity.java:153)
at java.lang.Thread.run(Thread.java:764)
谁能解释为什么我得到空点错误,因为我已经定义了所有变量并且我正在访问的数据库项也不为空?