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