Итак, у меня есть этот код, который работает нормально, «String question» иногда возвращает null, а иногда работает нормально, в чем может быть проблема? Data
Firebase localRef = new Firebase("https://commentcart.firebaseio.com/Pending Surveys/" + currUser
+ "/" + pendingSurveyKey);
localRef.addListenerForSingleValueEvent(new com.firebase.client.ValueEventListener() {
@Override
public void onDataChange(@NonNull com.firebase.client.DataSnapshot dataSnapshot) {
for (com.firebase.client.DataSnapshot d: dataSnapshot.getChildren())
{
String key = d.getKey();
String type = Objects.requireNonNull(dataSnapshot.child(key).child("Type").getValue()).toString();
String question = Objects.requireNonNull(dataSnapshot.child(key).child("Question").getValue()).toString();
String date = getCurrentDate();
surveysRefWithDates.child("Date").setValue(date);
switch (type)
{
case "Free":
case "Rating":
String answer = Objects.requireNonNull(dataSnapshot.child(key).child("Answer").getValue()).toString();
assert key != null;
surveysRef.child(key).child("Answer").setValue(answer);
surveysRef.child(key).child("Type").setValue(type);
surveysRef.child(key).child("Question").setValue(question);
surveysRefWithDates.child(key).child("Answer").setValue(answer);
surveysRefWithDates.child(key).child("Type").setValue(type);
surveysRefWithDates.child(key).child("Question").setValue(question);
break;
case "Multiple":
case "One":
String option1 = Objects.requireNonNull(dataSnapshot.child(key).child("Option1").getValue()).toString();
String option2 = Objects.requireNonNull(dataSnapshot.child(key).child("Option2").getValue()).toString();
String option3 = Objects.requireNonNull(dataSnapshot.child(key).child("Option3").getValue()).toString();
surveysRef.child(key).child("Option1").setValue(option1);
surveysRef.child(key).child("Option2").setValue(option2);
surveysRef.child(key).child("Option3").setValue(option3);
surveysRef.child(key).child("Type").setValue(type);
surveysRef.child(key).child("Question").setValue(question);
surveysRefWithDates.child(key).child("Option1").setValue(option1);
surveysRefWithDates.child(key).child("Option2").setValue(option2);
surveysRefWithDates.child(key).child("Option3").setValue(option3);
surveysRefWithDates.child(key).child("Question").setValue(question);
surveysRefWithDates.child(key).child("Type").setValue(type);
break;
}
}
progressDialog.dismiss();
mRef.child("Pending Surveys").child(currUser).child(pendingSurveyKey).removeValue()
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful())
{
AlertDialog.Builder builder = new AlertDialog.Builder(CustomerOneAnswerQuestion.this);
builder.setTitle(R.string.thank_you)
.setMessage(R.string.nice_day)
.setCancelable(false)
.setPositiveButton(R.string.go_to_home, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent goToHome = new Intent(getApplicationContext(), CustomerActivity.class);
goToHome.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(goToHome);
pendingPref.edit().clear().apply();
finish();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
else
{
Toast.makeText(CustomerOneAnswerQuestion.this,R.string.error_occurred,Toast.LENGTH_LONG).show();
}
}
});
}
@Override
public void onCancelled(@NonNull com.firebase.client.FirebaseError databaseError) {
}
});