我想在客户端保存服务器响应。我想在云端防火墙中为现有文档添加一个字段。这是我正在处理的代码。我在另一个项目中应用了相同的过程,它在那里工作正常。
public class Stripe extends AppCompatActivity {
FirebaseFirestore db;
String d;
private static final String TAG = "Stripe";
private FirebaseAuth mAuth;
Card card;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stripe);
Bundle bundle = getIntent().getExtras();
final int a= bundle.getInt("Amount");
System.out.println(a);
mAuth = FirebaseAuth.getInstance();
final CardInputWidget mCardInputWidget = (CardInputWidget) findViewById(R.id.card_input_widget);
Button button = (Button) findViewById(R.id.confirmation_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bundle bundle = getIntent().getExtras();
final int a= bundle.getInt("Amount");
System.out.println(a);
//final int a = Integer.parseInt(amt);
card = mCardInputWidget.getCard();
if (card == null) {
Log.e(TAG, "Invalid Card Data");
}
pay(a);
}
});
}
private void pay(final int a) {
com.stripe.android.Stripe stripe = new com.stripe.android.Stripe(getApplicationContext(), "*********************");
stripe.createToken(
card,
new TokenCallback() {
public void onSuccess(Token token) {
Toast.makeText(getApplicationContext(), token.getId(), Toast.LENGTH_LONG).show();
token.getId();
Map<String , Object> dataToSave = new HashMap<String, Object>();
final String token1 = token.getId().toString();
dataToSave.put("token",token1);
dataToSave.put("Amount",a);
FirebaseFirestore db = FirebaseFirestore.getInstance();
String UID = mAuth.getUid().toString();
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(" https://us-central1-deyapay-192704.cloudfunctions.net/stripePayMoney/")
.addConverterFactory(GsonConverterFactory.create(gson));
Retrofit retrofit = builder.build();
executeForm(token1,a,UID);
db.collection("deyaPayUsers").document(mAuth.getUid().toString()).collection("Stripe")
.add(dataToSave)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>(){
@Override
public void onSuccess(DocumentReference documentReference) {
Log.d(TAG, "DocumentSnapshot added with ID: " + documentReference.getId());
d= documentReference.getId();
System.out.println(d);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "error", e);
}
});
}
public void onError(Exception error) {
// Show localized error message
Toast.makeText(getApplicationContext(),
error.getLocalizedMessage(),
Toast.LENGTH_LONG
).show();
}
}
);
}
private void executeForm(String Token, Integer Amt, final String UID){
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("https://us-central1-deyapay-192704.cloudfunctions.net/stripePayMoney/")
.addConverterFactory(GsonConverterFactory.create(gson));
Retrofit retrofit = builder.build();
APIService apiservice=retrofit.create(APIService.class);
Call<PostData> call=apiservice.savePost(Token,Amt,UID);
call.enqueue(new Callback<PostData>() {
@Override
public void onResponse(Call<PostData> call, Response<PostData> response) {
if(response.isSuccessful()){
response.body();
String dp = response.body().getToken();
Log.d(TAG,dp);
System.out.println(d);
DocumentReference dbRef = db.collection("deyaPayUsers").document(UID).collection("Stripe").document(d);
dbRef
.update("TransactionId",dp)
.addOnSuccessListener(new OnSuccessListener<Void>(){
@Override
public void onSuccess(Void aVoid){
Log.d(TAG,"Documnetsnapshot successfully updated!");
}
})
.addOnFailureListener(new OnFailureListener(){
@Override
public void onFailure(@NonNull Exception e){
Log.w(TAG,"Error updating document",e);
}
});
Intent i = new Intent(Stripe.this,generic.class);
startActivity(i);
Toast.makeText(Stripe.this,"success",Toast.LENGTH_SHORT).show();
}
else
{
}
}
@Override
public void onFailure(Call<PostData> call, Throwable t) {
t.printStackTrace();
Log.e(TAG,t.toString());
}
});
}
}
但我遇到了这个错误 -
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'com.google.firebase.firestore.CollectionReference com.google.firebase.firestore.FirebaseFirestore.collection(java.lang.String)'
在DocumentReference dbRef行的onResponse类中抛出了此错误。
此处发生异常 -
DocumentReference dbRef = db.collection("deyaPayUsers").document(UID).collection("Stripe").document(d);
dbRef
.update("TransactionId",dp)
.addOnSuccessListener(new OnSuccessListener<Void>(){
@Override
public void onSuccess(Void aVoid){
Log.d(TAG,"Documnetsnapshot successfully updated!");
}
})
.addOnFailureListener(new OnFailureListener(){
@Override
public void onFailure(@NonNull Exception e){
Log.w(TAG,"Error updating document",e);
}
});
答案 0 :(得分:0)
根据@Rudiger的第1条评论,使用以下代码在d[0,1] = func(g[:,0], g[:,1])
方法中声明和分配db引用,但不在pay()
方法内。
execute()
有两种方法可以删除此NullPointerException:
您可以在您的班级中声明 // declare and initializing variable
FirebaseFirestore db = FirebaseFirestore.getInstance();
作为全局变量,并在FirebaseFirestore db
方法中初始化作为{{ 1}}在调用onCreate()
方法之前。
或者您也可以在db = FirebaseFirestore.getInstance();
方法中再次初始化并声明本地pay()
变量。
答案 1 :(得分:0)
FirebaseFirestore db;
未初始化并且正在executeForm()
方法中使用,因此您将获得NPE以便在此行进行收集参考
DocumentReference dbRef = db.collection("deyaPayUsers").document(UID).collection("Stripe").document(d);
要避免NPE在db
方法中初始化onCreate()
变量,如
db = FirebaseFirestore.getInstance();
现在,您的db
引用不为空,如果在您的活动中使用任何方法,则不会给您NPE。