0

I cannot find the error in my code while I am trying to delete a Recipe from the firebase Database and firebase Storage. Kindly help me in the right way. This is my code for deletion a recipe.

public class Detail_Activity extends AppCompatActivity {

    ImageView ivImage2;
    TextView txtDescription, txtName, txtPrice;
    String key = "";
    String imageUrl = "";
    Button delete_Recipe;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        ivImage2 = (ImageView)findViewById(R.id.ivImage2);
        txtDescription = (TextView) findViewById(R.id.txtDescription);
        txtName = (TextView) findViewById(R.id.txtName);
        txtPrice = (TextView) findViewById(R.id.txtPrice);
        delete_Recipe = (Button) findViewById(R.id.delete_Recipe);

        Bundle mBundle = getIntent().getExtras();
        if (mBundle != null){

            txtName.setText(mBundle.getString("Recipe Name"));
            txtPrice.setText(mBundle.getString("Recipe Price"));
            txtDescription.setText(mBundle.getString("Description"));
            key = mBundle.getString("keyValue");
            imageUrl = mBundle.getString("Image");

            Glide.with(this)
                    .load(mBundle.getString("Image"))
                    .into(ivImage2);
        }

        delete_Recipe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Recipe_Credential");
                FirebaseStorage storage = FirebaseStorage.getInstance();
                StorageReference storageReference = storage.getReferenceFromUrl(imageUrl);
                storageReference.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void unused) {
                        reference.child(key).removeValue();
                        Toast.makeText(Detail_Activity.this, "Recipe Delete!!!", Toast.LENGTH_LONG).show();
                        startActivity(new Intent(getApplicationContext(), Dashboard_Activity.class));
                        finish();
                    }
                });
            }
        });

    }
}

And in the logcat it indicate that Can't pass null for argument 'pathString' in child(). So how to solve this error. Kindly read the code and leave an answer.

2022-03-24 15:19:30.948 4081-4081/com.example.universityofloralaicafeteria E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.universityofloralaicafeteria, PID: 4081
    java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
        at com.google.firebase.database.DatabaseReference.child(DatabaseReference.java:96)
        at com.example.universityofloralaicafeteria.Detail_Activity$1$1.onSuccess(Detail_Activity.java:62)
        at com.example.universityofloralaicafeteria.Detail_Activity$1$1.onSuccess(Detail_Activity.java:59)
        at com.google.android.gms.tasks.zzm.run(com.google.android.gms:play-services-tasks@@18.0.1:1)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8653)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Jabbar Kakar
  • 11
  • 1
  • 2

0 Answers0