-1

I get the following Null Pointer exception when I try to save an image in Google Drive:

Attempt to invoke virtual method 'com.google.api.services.drive.Drive$Files com.google.api.services.drive.Drive.files()' on a null object reference
                          java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.api.services.drive.Drive$Files com.google.api.services.drive.Drive.files()' on a null object reference
                              at com.amrutpatil.makeanote.GDActions.search(GDActions.java:208)
                              at com.amrutpatil.makeanote.NotesActivity$2.run(NotesActivity.java:154)

GDActions.java:

I first intialize the GoogleApiClient in the init method and then try to get the files in the search method.

static void init(NotesActivity ctx, String email) {
        if (ctx != null && email != null) {
            mGAC = new GoogleApiClient.Builder(ctx).addApi(Drive.API)
                    .addScope(Drive.SCOPE_FILE).setAccountName(email)
                    .addConnectionCallbacks(ctx).addOnConnectionFailedListener(ctx).build();

            mGOOSvc = new com.google.api.services.drive.Drive.Builder(
                    AndroidHttp.newCompatibleTransport(), new GsonFactory(), GoogleAccountCredential
                    .usingOAuth2(ctx, Arrays.asList(DriveScopes.DRIVE_FILE))
                    .setSelectedAccountName(email)
            ).build();
        }
    }

static ArrayList<GF> search(String prId, String titl, String mime) {
            ArrayList<GF> gfs = new ArrayList<>();
            String qryClause = "'me' in owners and ";
            if (prId != null) qryClause += "'" + prId + "' in parents and ";
            if (titl != null) qryClause += "title = '" + titl + "' and ";
            if (mime != null) qryClause += "mimeType = '" + mime + "' and ";
            qryClause = qryClause.substring(0, qryClause.length() - " and ".length());
            com.google.api.services.drive.Drive.Files.List qry = null;
            try {
                qry = mGOOSvc.files().list().setQ(qryClause)
                        .setFields("items(id, labels/trashed, title), nextPageToken");   //Error here
                gfs = search(gfs, qry);
            } catch (GoogleAuthIOException gaiEx) {
                try {
                    gfs = search(gfs, qry);
                    gaiEx.printStackTrace();
                } catch (Exception g) {
                    GDUT.le(g);
                }
            } catch (Exception e) {
                GDUT.le(e);
            }
            return gfs;
        }

NotesActivity.java

@Override
    public void onLoadFinished(Loader<List<Note>> loader, List<Note> data) {
        this.mNotes = data;

        Thread[] threads = new Thread[mNotes.size()];
        int threadCounter = 0;

        for(final Note aNote : mNotes){

            if(AppConstant.GOOGLE_DRIVE_SELECTION == aNote.getStorageSelection()){
                GDUT.init(this);

                if(checkPlayServices() && checkUserAccount()){
                    GDActions.init(this, GDUT.AM.getActiveEmil());
                    GDActions.connect(true);
                }

                threads[threadCounter] = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        do{
                            //Get the image file
                            ArrayList<GDActions.GF> gfs = GDActions.search(AppSharedPreferences.getGoogleDriveResourceId(getApplicationContext()),
                                    aNote.getImagePath(), GDUT.MIME_JPEG);   //Error here

                            if(gfs.size() > 0){
                                //Retrieve the file, convert it into Bitmap to display on the screen
                                byte[] imageBytes = GDActions.read(gfs.get(0).id, 0);


                                Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
                                aNote.setBitmap(bitmap);
                                mIsImageNotFound = false;
                                mNotesAdapter.setData(mNotes);
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {

                                        mNotesAdapter.notifyImageObtained();
                                    }
                                });
                            } else{
                                aNote.setBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_loading));
                                mIsImageNotFound = true;
                                try{
                                    Thread.sleep(500);
                                } catch (InterruptedException e){
                                    e.printStackTrace();
                                }
                            }
                        }while(mIsImageNotFound);
                    }
                });
                threads[threadCounter].start();
                threadCounter++;
            } else {
                aNote.setHasNoImage(true);
            }
        }
        mNotesAdapter.setData(mNotes);  
        changeNoItemTag();
    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
ap6491
  • 805
  • 1
  • 10
  • 26
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – RaminS Mar 27 '16 at 21:05
  • Have you tested that GDUT.AM.getActiveEmil() doesn't return null? – RubioRic Mar 27 '16 at 21:11
  • Yes, I did test that getActiveEmil method does not return a null. Currently, it does return a valid email. – ap6491 Mar 27 '16 at 21:30

1 Answers1

0

Turns out that there was a permissions issue. Although I had GET_ACCOUNTS permission set in my AndroidManifest.xml file, I needed to manually enable Contacts permissions in my app settings.

It may be a device specific issue as I was running my app on a Nexus 6 physical device with Android Marshmallow 6.0.1.

You might want to check you app settings just to be safe.

ap6491
  • 805
  • 1
  • 10
  • 26