I'm trying to give a database reference to upload some data to my Firebase real-time database. I already have some data in the database and I'm retrieving it. I have a spinner in which the User can select a specific user to whom he wants to send the message. So I want to upload the data into the selected user's UID.
I tried retrieving the UID of the selected user directly but it didn't work, so I created a child called UID during the registration process. Now I'm able to retrieve the UID of the selected User and I'm storing it in String 'key'. I want to use 'key' as a child reference in database reference, so that I can upload my data under the selected user. I have checked the value of 'key' and it is the correct UID i.e., the UID of the selected user, but when I use this String in database reference, my app crashes.
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {
Object item = adapterView.getItemAtPosition(position);
NameSelected = item.toString();
pris = NameSelected;
ref = FirebaseDatabase.getInstance().getReference().child("Faculty_Database");
ref.orderByChild("Name").equalTo(pris).addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
key = datas.child("UID").getValue(String.class);
}
}
//
SubmitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (NameEditText.getText().toString().equals("") || PhoneNumberEditText.getText().toString().equals("") || CompanyEditText.getText().toString().equals("") || EmailEditText.getText().toString().equals("") || AppointmentEditText.getText().toString().equals("") || image.equals(null)) {
Toast.makeText(getApplicationContext(), "Please fill all details", Toast.LENGTH_LONG).show();
} else {
final StudentDetails studentDetails = new StudentDetails();
GetDataFromEditText();
studentDetails.setName(NameHolder);
studentDetails.setPhoneNumber(NumberHolder);
studentDetails.setemail(EmailHolder);
studentDetails.setCompanyName(CompanyHolder);
studentDetails.setAppointment(AppointmentHolder);
studentDetails.setNameSelected(NameSelected);
// Getting the ID from firebase database.
final String StudentRecordIDFromServer = databaseReference.push().getKey();
filePath = FirebaseStorage.getInstance().getReference().child("Visitors");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] dataToUpload = baos.toByteArray();
UploadTask uploadTask = filePath.putBytes(dataToUpload);
Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
// Continue with the task to get the download URL
return filePath.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
downloadUri= task.getResult();
databaseReference = FirebaseDatabase.getInstance().getReference().child("Faculty_Database").child(key).child("Received");
Map<String, Object> mapToUpload = new HashMap<>();
mapToUpload.put("imageUrl", downloadUri.toString());
mapToUpload.put("Data", studentDetails);
databaseReference.child(StudentRecordIDFromServer).setValue(mapToUpload);
} else {
// Handle failures
// ...
}
}
});
}
// Showing Toast message after successfully data submit.
Toast.makeText(SecurityActivity.this, "Request Sent Successfully!", Toast.LENGTH_LONG).show();
}
});
This is my database

Please let me know how I can create a child called "Receieved" under the selected User's UID to upload my data?