简体   繁体   中英

file object methods throws NullPointerException - Google Drive API

After configure and successfully test quickstart code in https://developers.google.com/drive/v3/web/quickstart/java

I'm receiving NullPointerException after trying other file methods eg: getFileExtension(), get WebViewLink(), get Md5 Checksum() even when file.getName() and file.getId() works fine.

Original working code:

public static void main(String[] args) throws IOException {
    // Build a new authorized API client service.
    Drive service = getDriveService();

    // Print the names and IDs for up to 10 files.
    FileList result = service.files().list()
         .setPageSize(10)
         .setFields("nextPageToken, files(id, name)")
         .execute();
    List<File> files = result.getFiles();
    if (files == null || files.size() == 0) {
        System.out.println("No files found.");
    } else {
        System.out.println("Files:");
        for (File file : files) {
            System.out.printf("%s (%s)\n", file.getName(), file.getId());
        }
    }
}

I'll appreciate any help to understand Null Pointer reason calling other methods on file objects. Thanks a lot

You need to put all the fields you need in .setFields("nextPageToken, files(id, name)") . Right now, you just have id and name . So, you are getting a partial response having just the id and name and so everything else is null and therefore you are getting NullPointerException . If you want to get the full response containing all data about the files, try .setFields("nextPageToken, files") .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM