avichai80
avichai80

Reputation: 1

How An use of all images from the parse

I need to pull all the image from parse. this is the error in the logCat: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference

The error is here:

String ObjectId =  ParseObjects.get(position).getObjectId();

This is my code:

public void PullImage(final ImageView image,int position){
    progressDialog = ProgressDialog.show(mContext, "", "Downloading Image...", true);
    String ObjectId =  ParseObjects.get(position).getObjectId();
    query.getInBackground(ObjectId, new GetCallback < ParseObject > () {
        public void done (ParseObject object, ParseException e){
            if (object != null) {
                ParseFile file = (ParseFile) object.get("ImageFile");
                file.getDataInBackground(new GetDataCallback() {
                    public void done(byte[] data, ParseException e) {
                        if (e == null) {
                            Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                            // surface.addImage(bitmap);

                            // float Xplace  = targetImageforpull.getX();
                            // float Yplace  = targetImageforpull.getY();

                            image.setImageBitmap(bitmap);
                            //use this bitmap as you want
                            progressDialog.dismiss();
                        } else {
                            // something went wrong
                        }
                    }
                });
            } else {
                Toast.makeText(mContext, "Exception", Toast.LENGTH_SHORT).show();
            }
        }
    });
}

I know that my problem is here:

public void SetParse(){
    query = new ParseQuery<ParseObject>("User");
    //  query.whereEqualTo("Column", bitmap);
     size = 0;
    try {
        size = query.count();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> objects, ParseException e) {
            if (e == null) {
                ParseObjects = objects;
                // The query was successful.
            } else {
                // Something went wrong.
            }
        }
    });
}

all code:

public class CustomGrid extends BaseAdapter {
    private Context mContext;
    private Bitmap btimaprecieve;
    List<ParseObject> ParseObjects;

    ParseQuery<ParseObject> query;

    ProgressDialog progressDialog;

    int size;

    public CustomGrid(Context c) {
        mContext = c;
        SetParse();
    }

    //---returns the number of images---
    public int getCount() {
        return size;
    }

    //---returns the ID of an item---
    public Object getItem(int position) {
        return imageId[position];
    }

    public long getItemId(int position) {
        return position;
    }

    //---returns an ImageView view---
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) { // if it's not recycled, initialize some
            // attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(120, 120));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(25, 25, 25, 25);
        } else {
            imageView = (ImageView) convertView;
        }
        PullImage(imageView,position);
     //   imageView.setImageResource(imageId[position]);

        return imageView;
    }

    public void SetParse(){
        query = new ParseQuery<ParseObject>("User");
        //  query.whereEqualTo("Column", bitmap);
         size = 0;
        try {
            size = query.count();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        query.findInBackground(new FindCallback<ParseObject>() {

            public void done(List<ParseObject> objects, ParseException e) {
                if (e == null) {
                   // ParseObjects = objects;
                     ParseObjects = new ArrayList<ParseObject>(objects);
                    if(ParseObjects == null){
                        Log.d("ParseObjects is null ", "ParseObjects is null");
                    }
                    if(objects == null){
                        Log.d("objects is null ", "objects is null");
                    }

                    // The query was successful.
                } else {
                    // Something went wrong.
                }
            }
        });
    }

    public void PullImage(final ImageView image,int position){

        progressDialog = ProgressDialog.show(mContext, "", "Downloading Image...", true);
        String ObjectId =  ParseObjects.get(position).getObjectId();
        query.getInBackground(ObjectId, new GetCallback < ParseObject > () {
            public void done (ParseObject object, ParseException e){
                if (object != null) {

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

                    file.getDataInBackground(new GetDataCallback() {


                        public void done(byte[] data, ParseException e) {
                            if (e == null) {

                                Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                                // surface.addImage(bitmap);

                                // float Xplace  = targetImageforpull.getX();
                                // float Yplace  = targetImageforpull.getY();

                                image.setImageBitmap(bitmap);
                                //use this bitmap as you want
                                progressDialog.dismiss();
                            } else {
                                // something went wrong
                            }
                        }
                    });

                } else {
                    Toast.makeText(mContext, "Exception", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private Integer [] imageId = {
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
    };
    public Bitmap add(Bitmap bitmap_recieve) {
        return btimaprecieve;
    }
}

Upvotes: 0

Views: 155

Answers (1)

once2go
once2go

Reputation: 1500

i'm use glide(or any other lib(volley, picasa) for loading ing by URL)

    userQuery.getFirstInBackground(new GetCallback<ParseObject>() {
        @Override
        public void done(ParseObject parseObject, ParseException e) {
            if((ParseFile) parseObject.get("photo")!= null){
                Glide.with(context).load(((ParseFile) parseObject.get("photo")).getUrl()).asBitmap().dontAnimate().centerCrop().transform(new CircleTransform(context))
                        .diskCacheStrategy(DiskCacheStrategy.SOURCE).into(hd.logo);
            }
        }
    });

If you need bitmap u can fat it after loading from your img view. be carefully, if your callback returns ParseArray, u must get object with your picture by position like this ParseObjects.get(position).getObjectId(); But if you have ParseObject you must write ParseObjects.getObjectId();(without get(position)). Check your callback.

Upvotes: 1

Related Questions