我正在尝试将数据推送到AWS DynamoDB,但我一直面临这个错误:
java.lang.NullPointerException:尝试调用虚方法com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper com.uploadtodatabasevts.amazonaws.AWSMobileClient.getDynamoDBMapper()'在空对象引用上
对于遗漏的内容或错误输入的内容有任何想法吗?我似乎无法找到答案。
以下是nil
类:
对于MainActivity
,假设它是主键自动增量,但我不知道如何在AWS DynamoDB NoSQL上执行此操作,因此我目前正在手动推送ID。
outfitID

以下是AWSMobileClient:
package com.uploadtodatabasevts;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.amazonaws.AmazonClientException;
import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper;
import com.uploadtodatabasevts.amazonaws.AWSMobileClient;
import com.uploadtodatabasevts.amazonaws.DynamoDBUtils;
import com.uploadtodatabasevts.amazonaws.OutfitDO;
import com.uploadtodatabasevts.amazonaws.ThreadUtils;
public class MainActivity extends AppCompatActivity {
private static final String LOG_TAG = MainActivity.class.getName();
String pID, pDetails;
double oID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText outfitID = (EditText)findViewById(R.id.eTxtOutfitID);
EditText productID = (EditText)findViewById(R.id.eTxtProductID);
pID = productID.getText().toString();
EditText productDetails = (EditText)findViewById(R.id.eTxtProductDetails);
pDetails = productDetails.getText().toString();
Button confirm = (Button)findViewById(R.id.btnConfirm);
confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
insertData();
}
});
}
private void insertData() throws AmazonClientException {
final DynamoDBMapper dynamoDBMapper = AWSMobileClient.defaultMobileClient().getDynamoDBMapper();
final OutfitDO outfitDO = new OutfitDO();
outfitDO.setOutfitId((double) 10);
outfitDO.setOutfitId(oID);
outfitDO.setItemOneId(pID);
outfitDO.setItemOneDetails(pDetails);
AmazonClientException lastException = null;
try {
dynamoDBMapper.save(outfitDO);
} catch (final AmazonClientException ex) {
Log.e(LOG_TAG, "Failed saving item : " + ex.getMessage(), ex);
lastException = ex;
}
if (lastException != null){
throw lastException;
}
}
}

答案 0 :(得分:1)
您的Application类应具有通过调用AWSMobileClient.initializeMobileClientIfNecessary(context)
来初始化移动客户端的代码,否则AWSMobileClient.defaultMobileClient()将返回null。如果您没有自定义Application类,则可以在Activity的AWSMobileClient.initializeMobileClientIfNecessary(context)
生命周期方法中调用onCreate()
。