‘java.lang.String java.lang.Object.toString()’在空对象引用上。

huangapple 未分类评论44阅读模式
英文:

'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&lt;Void&gt;() {
                    @Override
                    public void onComplete(@NonNull Task&lt;Void&gt; 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, &quot;Product is added successfully...&quot;, Toast.LENGTH_SHORT).show();

                        }

                        else{

                            loadingBar.dismiss();
                            String message = task.getException().toString();
                            Toast.makeText(AdminAddNewProductActivity.this, &quot;Error: &quot;+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();
}

huangapple
  • 本文由 发表于 2020年4月10日 16:48:21
  • 转载请务必保留本文链接:https://java.coder-hub.com/61136899.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定