0

In this code I got the error of Can't pass null for argument 'pathString' in child(). I don't know what is wrong here. In the following line of code I faced error that there is null key:

databaseReference = FirebaseDatabase.getInstance().getReference("Recipe_Credentials").child(key);

public class Update_Activity extends AppCompatActivity {
    
        ImageView update_Recipe_Image;
        EditText update_Recipe_Name, update_Recipe_Description, update_Recipe_Price;
        Button updatebutton, selectImageButton;
        String key, oldimageUrl;
        String imageUrl;
        DatabaseReference databaseReference;
        StorageReference storageReference;
        String recipename, recipedescription, recipeprice;
    
        private Uri filePath;
        private final int PICK_IMAGE_REQUEST = 10;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_update);
    
            update_Recipe_Image = (ImageView) findViewById(R.id.update_Recipe_Image);
            update_Recipe_Name = (EditText) findViewById(R.id.update_Recipe_Name);
            update_Recipe_Description = (EditText) findViewById(R.id.update_Recipe_Description);
            update_Recipe_Price = (EditText) findViewById(R.id.update_Recipe_Price);
            updatebutton = (Button) findViewById(R.id.updatebutton);
            selectImageButton = (Button) findViewById(R.id.selectImageButton);
    
            databaseReference = FirebaseDatabase.getInstance().getReference("Recipe_Credentials").child(key);
    
            Bundle bundle = getIntent().getExtras();
            if (bundle != null){
                Glide.with(Update_Activity.this)
                        .load(bundle.getString("oldimageUrl"))
                        .into(update_Recipe_Image);
                update_Recipe_Name.setText(bundle.getString("recipeNameKey"));
                update_Recipe_Description.setText(bundle.getString("recipeDescriptionKey"));
                update_Recipe_Price.setText(bundle.getString("recipePriceKey"));
                key = bundle.getString("key");
                oldimageUrl = bundle.getString("oldimageUrl");
    
            }
    
            selectImageButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    chooseImage();
                }
            });
    
            updatebutton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                     recipename = update_Recipe_Name.getText().toString().trim();
                     recipedescription = update_Recipe_Description.getText().toString().trim();
                     recipeprice = update_Recipe_Price.getText().toString().trim();
                     uploadImage();
                }
            });
        }

Here is the logcat message showing the following error

Caused by: 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.Admin.Update_Activity.onCreate(Update_Activity.java:62)
        at android.app.Activity.performCreate(Activity.java:7183)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)

Kindly help i resolving the error.

Jabbar Kakar
  • 11
  • 1
  • 2

1 Answers1

0
//databaseReference = FirebaseDatabase.getInstance().getReference("Recipe_Credentials").child(key);

Bundle bundle = getIntent().getExtras();
        if (bundle != null){
    Glide.with(Update_Activity.this)
            .load(bundle.getString("oldimageUrl"))
            .into(update_Recipe_Image);
    update_Recipe_Name.setText(bundle.getString("recipeNameKey"));
    update_Recipe_Description.setText(bundle.getString("recipeDescriptionKey"));
    update_Recipe_Price.setText(bundle.getString("recipePriceKey"));
    key = bundle.getString("key");
    oldimageUrl = bundle.getString("oldimageUrl");

}

// Init databaseReference after getting key value 

databaseReference = FirebaseDatabase.getInstance().getReference("Recipe_Credentials").child(key);

You have to init database reference after getting data from bundle

Gulab Sagevadiya
  • 872
  • 1
  • 8
  • 20