AER
AER

Reputation: 95

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 e.g.: 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

Upvotes: 0

Views: 374

Answers (1)

Shakhar
Shakhar

Reputation: 442

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").

Upvotes: 1

Related Questions