首页 > 解决方案 > java.lang.String android.net.Uri.toString() 在空对象引用上,LogCat 在第 202 行显示此错误

问题描述

我试图根据存储引用的图像路径是否为空的条件来获取下载 URL。

我试图将整个代码放在 if else 语句中,而不是在解决错误的条件内将值分配给 download_uri。我无法理解为什么这种方法有效而另一种方法无效。

    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);

我希望得到 downLoad_uri 但它提供了一个空值

标签: javaandroidfirebasenullpointerexceptionfirebase-storage

解决方案


如果您可以通过简单的在线代码来完成,我不知道您为什么要做不必要的工作来获取图像的下载 URL。

只需使用以下代码将图像上传到 Firebase 存储。

    image_path.putFile(mainImageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
       @Override
       public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
         if(task.isSuccessful()){

           // HERE YOU CAN DIRECTLY GET DOWNLOAD URL OF UPLOADED IMAGE USING TASK RESULT.
           String downloadUrl = String.valueOf(taskSnapshot.getDownloadUrl());
           storeFireStore(downloadUrl,name);

         } else {

           String error = task.getException().getMessage();
           Toast.makeText(SetUpActivity.this,"Image Error: "+error,Toast.LENGTH_SHORT).show();

         }
         mProgressBar.setVisibility(View.INVISIBLE);
       }
    });

并改变storeFireStore如下方法,

    private void storeFireStore(String image_path, final String name) {

        Map<String, String> userMap = new HashMap<>();
        userMap.put("image", image_path != null ? image_path : "");//line 202 this is where the error occurs.
        userMap.put("name", name);

    }

推荐阅读