java.lang.String android.net.Uri.toString() on a null object reference, LogCat shows this error at line 202
I am trying to get the download Url based on the condition that the image path for the storage reference is null or not.
I tried to put the whole code inside the if else statement instead of just assigning the value to download_uri inside the condition which resolves the error. I am not able to understand why this approach works and another one doesn't.
mSaveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String name=mProfileName.getText().toString();
if(!TextUtils.isEmpty(name) && mainImageUri!=null){
mProgressBar.setVisibility(View.VISIBLE);
if(isChanged){
//The task of storing the data goes through uploading the image as well.
user_id = mUser.getUid();
final StorageReference image_path= mStorageReference.child("profile_photo").child(user_id+".jpg");
image_path.putFile(mainImageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if(task.isSuccessful()){
storeFireStore(image_path,name);
}else{
String error = task.getException().getMessage();
Toast.makeText(SetUpActivity.this,"Image Error: "+error,Toast.LENGTH_SHORT).show();
}
mProgressBar.setVisibility(View.INVISIBLE);
}
});
}else{
//The task of storing the data does not go through uploading the image.
storeFireStore(null,name);
mProgressBar.setVisibility(View.INVISIBLE);
}
}
}
});
}
private void storeFireStore(StorageReference image_path, final String name) {
final Uri[] download_uri = new Uri[1];
if(image_path!=null){
image_path.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
download_uri[0] =uri;
Log.d(TAG,"Download Url obtained");
}
});
} else{
download_uri[0] =mainImageUri;
Log.d(TAG,"Download Url obtained in else");
}
Map<String,String> userMap=new HashMap<>();
userMap.put("image", download_uri[0].toString());//line 202 this is
where the error occurs.userMap.put("name",name);
I expect to get the downLoad_uri but it provides a null value
Source: View source