英文:
'java.lang.String java.lang.Object.toString()' on a null object reference
问题
ProductRef.child(productRandomKey).updateChildren(productMap)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
downloadImageUrl = task.getResult().toString(); //it is the problem
Intent intent = new Intent(AdminAddNewProductActivity.this, AdminCategoryActivity.class);
startActivity(intent);
loadingBar.dismiss();
Toast.makeText(AdminAddNewProductActivity.this, "Product is added successfully...", Toast.LENGTH_SHORT).show();
}
else{
loadingBar.dismiss();
String message = task.getException().toString();
Toast.makeText(AdminAddNewProductActivity.this, "Error: "+message, Toast.LENGTH_SHORT).show();
}
}
});
final StorageReference filePath = ProductImageRef.child(ImageUri.getLastPathSegment() + productRandomKey + ".jpg");
final UploadTask uploadTask = filePath.putFile(ImageUri);
downloadImageUrl = filePath.getDownloadUrl().toString();
return filePath.getDownloadUrl();
英文:
ProductRef.child(productRandomKey).updateChildren(productMap)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
downloadImageUrl = task.getResult().toString(); //it is the problem
Intent intent = new Intent(AdminAddNewProductActivity.this, AdminCategoryActivity.class);
startActivity(intent);
loadingBar.dismiss();
Toast.makeText(AdminAddNewProductActivity.this, "Product is added successfully...", Toast.LENGTH_SHORT).show();
}
else{
loadingBar.dismiss();
String message = task.getException().toString();
Toast.makeText(AdminAddNewProductActivity.this, "Error: "+message, Toast.LENGTH_SHORT).show();
}
}
});
final StorageReference filePath = ProductImageRef.child(ImageUri.getLastPathSegment() + productRandomKey + ".jpg");
final UploadTask uploadTask = filePath.putFile(ImageUri);
downloadImageUrl = filePath.getDownloadUrl().toString();
return filePath.getDownloadUrl();
I have used these lines previously. it is occurring when I am trying to upload a picture the app crash.
答案1
得分: 0
你正在对 null
对象调用 toString();
。因此,你可以添加一些条件来进行检查。将这段代码进行修改:
Result result = task.getResult();
if (result == null) {
// TODO - 在此处理 null 对象(例如显示一些占位内容)
} else {
downloadImageUrl = result.toString();
}
英文:
You are calling toString();
on null
object. So you can add some condition to check it. Change this:
downloadImageUrl = task.getResult().toString();
to
Result result = task.getResult();
if (result == null) {
// TODO - handle null object here (e.g. show some placeholder)
} else {
downloadImageUrl = result.toString();
}
专注分享java语言的经验与见解,让所有开发者获益!
评论