尝试在空对象引用上调用虚拟方法'void com.parse.ParseFile.getDataInBackgroundcom.parse.GetDataCallback'

问题描述

伙计们,我尝试从解析服务器下载图像并将其设置到导航抽屉的图像视图中,但是当它尝试在后台file.getDataInBackground查找文件时,出现此错误,我应该这样做

public void queryForUserPic() {
        try{
            ParseQuery<ParSEObject> query = ParseQuery.getQuery ("Images");
            query.whereEqualTo("userId",ParseUser.getCurrentUser().getobjectId());
            query.findInBackground(new FindCallback<ParSEObject>() {
                @Override
                public void done(List<ParSEObject> objects,ParseException e) {
                   if(e==null &&objects.size()>0){
                       for (ParSEObject object : objects) {
                           ParseFile file = (ParseFile) object.get("Image");
                           Log.i("test",object.get("userId").toString());
                           file.getDataInBackground(new GetDataCallback() {
                               @Override
                               public void done(byte[] data,ParseException e) {
                                    if(e==null& data != null){
                                        Bitmap bitmap =BitmapFactory.decodeByteArray(data,data.length);
                                        profile_image.setimageBitmap(bitmap);
                                    }
                               }
                           });
                       }
                   }
                }
            });

        }catch (Exception e){
            e.printstacktrace();
        }

    }

这是错误

   java.lang.NullPointerException: Attempt to invoke virtual method 'void com.parse.ParseFile.getDataInBackground(com.parse.GetDataCallback)' on a null object reference

解决方法

更改所有将“ userId”写入“ objectId”的位置,因为对象ID存储在“ objectId”列中,并且由于编译器无法找到任何名为“ userId”的列而出现错误。 / p>

希望它对您有用!

,

您正试图在getDataInBackground文件对象引用上调用null方法。在您的情况下,以下file对象为空:

ParseFile file = (ParseFile) object.get("Image");

您可以显式检查是否为空:

for (ParseObject object : objects) {
                ParseFile file = (ParseFile) object.get("Image");
                Log.i("test",object.get("userId").toString());
                if (file != null) { //explicite check for null
                    file.getDataInBackground(new GetDataCallback() {
                        @Override
                        public void done(byte[] data,ParseException e) {
                            if(e==null& data != null){
                                Bitmap bitmap = BitmapFactory.decodeByteArray(data,data.length);
                                profile_image.setImageBitmap(bitmap);
                            }
                        }
                    });   
                }
            }