簡體   English   中英

文件對象方法拋出NullPointerException-Google Drive API

[英]file object methods throws NullPointerException - Google Drive API

https://developers.google.com/drive/v3/web/quickstart/java中配置並成功測試快速入門代碼之后

我嘗試了其他文件方法(例如,getFileExtension(),WebViewLink(),Md5 Checksum())后收到NullPointerException,即使file.getName()和file.getId()工作正常。

原始工作代碼:

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

我將不勝感激任何了解Null Pointer原因的文件對象調用其他方法的原因。 非常感謝

您需要將所有需要的字段放在.setFields("nextPageToken, files(id, name)") 現在,您只有idname 因此,您將獲得僅包含idname的部分響應,因此其他所有內容均為null ,因此您將獲得NullPointerException 如果要獲取包含有關文件的所有數據的完整響應,請嘗試.setFields("nextPageToken, files")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM