我試圖將數據推到AWS DynamoDB調用虛擬方法,但我一直面臨着這樣的錯誤:錯誤 - 嘗試在空對象引用
java.lang.NullPointerException: Attempt to invoke virtual method 'com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper com.uploadtodatabasevts.amazonaws.AWSMobileClient.getDynamoDBMapper()' on a null object reference
什麼是錯過了還是什麼做任何想法我輸入錯誤?我似乎無法找到答案。
下面是MainActivity
類:
爲outfitID
其假設是主鍵自動遞增,但我不知道如何做到這一點的AWS DynamoDB NoSQL的,所以我目前正在力推的ID手動。
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;
}
}
}
下面是AWSMobileClient:
package com.uploadtodatabasevts.amazonaws;
import android.content.Context;
import android.util.Log;
import com.amazonaws.ClientConfiguration;
import com.uploadtodatabasevts.amazonaws.IdentityManager;
import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
/**
* The AWS Mobile Client bootstraps the application to make calls to AWS
* services. It creates clients which can be used to call services backing the
* features you selected in your project.
*/
public class AWSMobileClient {
private static final String LOG_TAG = AWSMobileClient.class.getSimpleName();
private static AWSMobileClient instance;
private final Context context;
private ClientConfiguration clientConfiguration;
private IdentityManager identityManager;
private AmazonDynamoDBClient dynamoDBClient;
private DynamoDBMapper dynamoDBMapper;
/**
* Build class used to create the AWS mobile client.
*/
public static class Builder {
private Context applicationContext;
private String cognitoIdentityPoolID;
private Regions cognitoRegion;
private ClientConfiguration clientConfiguration;
private IdentityManager identityManager;
\t /**
\t * Constructor.
\t * @param context Android context.
\t */
public Builder(final Context context) {
this.applicationContext = context.getApplicationContext();
};
\t /**
\t * Provides the Amazon Cognito Identity Pool ID.
\t * @param cognitoIdentityPoolID identity pool ID
\t * @return builder
\t */
public Builder withCognitoIdentityPoolID(final String cognitoIdentityPoolID) {
this.cognitoIdentityPoolID = cognitoIdentityPoolID;
return this;
};
\t /**
\t * Provides the Amazon Cognito service region.
\t * @param cognitoRegion service region
\t * @return builder
\t */
public Builder withCognitoRegion(final Regions cognitoRegion) {
this.cognitoRegion = cognitoRegion;
return this;
}
/**
* Provides the identity manager.
\t * @param identityManager identity manager
\t * @return builder
\t */
public Builder withIdentityManager(final IdentityManager identityManager) {
this.identityManager = identityManager;
return this;
}
/**
* Provides the client configuration
* @param clientConfiguration client configuration
* @return builder
*/
public Builder withClientConfiguration(final ClientConfiguration clientConfiguration) {
this.clientConfiguration = clientConfiguration;
return this;
}
\t /**
\t * Creates the AWS mobile client instance and initializes it.
\t * @return AWS mobile client
\t */
public AWSMobileClient build() {
return
new AWSMobileClient(applicationContext,
cognitoIdentityPoolID,
cognitoRegion,
identityManager,
clientConfiguration);
}
}
private AWSMobileClient(final Context context,
final String cognitoIdentityPoolID,
final Regions cognitoRegion,
final IdentityManager identityManager,
final ClientConfiguration clientConfiguration) {
this.context = context;
this.identityManager = identityManager;
this.clientConfiguration = clientConfiguration;
this.dynamoDBClient = new AmazonDynamoDBClient(identityManager.getCredentialsProvider(), clientConfiguration);
this.dynamoDBClient.setRegion(Region.getRegion(AWSConfiguration.AMAZON_DYNAMODB_REGION));
this.dynamoDBMapper = new DynamoDBMapper(dynamoDBClient);
}
/**
* Sets the singleton instance of the AWS mobile client.
* @param client client instance
*/
public static void setDefaultMobileClient(AWSMobileClient client) {
instance = client;
}
/**
* Gets the default singleton instance of the AWS mobile client.
* @return client
*/
public static AWSMobileClient defaultMobileClient() {
return instance;
}
/**
* Gets the identity manager.
* @return identity manager
*/
public IdentityManager getIdentityManager() {
return this.identityManager;
}
/**
* Creates and initialize the default AWSMobileClient if it doesn't already
* exist using configuration constants from {@link AWSConfiguration}.
*
* @param context an application context.
*/
public static void initializeMobileClientIfNecessary(final Context context) {
if (AWSMobileClient.defaultMobileClient() == null) {
Log.d(LOG_TAG, "Initializing AWS Mobile Client...");
final ClientConfiguration clientConfiguration = new ClientConfiguration();
clientConfiguration.setUserAgent(AWSConfiguration.AWS_MOBILEHUB_USER_AGENT);
final IdentityManager identityManager = new IdentityManager(context, clientConfiguration);
final AWSMobileClient awsClient =
new AWSMobileClient.Builder(context)
.withCognitoRegion(AWSConfiguration.AMAZON_COGNITO_REGION)
.withCognitoIdentityPoolID(AWSConfiguration.AMAZON_COGNITO_IDENTITY_POOL_ID)
.withIdentityManager(identityManager)
.withClientConfiguration(clientConfiguration)
.build();
AWSMobileClient.setDefaultMobileClient(awsClient);
}
Log.d(LOG_TAG, "AWS Mobile Client is OK");
}
/**
* Gets the DynamoDB Client, which allows accessing Amazon DynamoDB tables.
* @return the DynamoDB client instance.
*/
public AmazonDynamoDBClient getDynamoDBClient() {
return dynamoDBClient;
}
/**
* Gets the Dynamo DB Object Mapper, which allows accessing DynamoDB tables using annotated
* data object classes to represent your data using POJOs (Plain Old Java Objects).
* @return the DynamoDB Object Mapper instance.
*/
public DynamoDBMapper getDynamoDBMapper() {
return dynamoDBMapper;
}
}
可能重複[什麼是NullPointerException,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-doi-i-fix -it) –
發佈'AWSMobileClient'類的代碼。 –
AWSMobileClient.defaultMobileClient()返回一個空值似乎很清楚。但是,由於您沒有發佈該代碼,因此無法說明原因。如果閱讀代碼並不明顯,那麼您的第一步應該是使用調試器來找出原因。 – GreyBeardedGeek