简体   繁体   English

文件对象方法抛出NullPointerException-Google Drive API

[英]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 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. 我尝试了其他文件方法(例如,getFileExtension(),WebViewLink(),Md5 Checksum())后收到NullPointerException,即使file.getName()和file.getId()工作正常。

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. 我将不胜感激任何了解Null Pointer原因的文件对象调用其他方法的原因。 Thanks a lot 非常感谢

You need to put all the fields you need in .setFields("nextPageToken, files(id, name)") . 您需要将所有需要的字段放在.setFields("nextPageToken, files(id, name)") Right now, you just have id and name . 现在,您只有idname 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 . 因此,您将获得仅包含idname的部分响应,因此其他所有内容均为null ,因此您将获得NullPointerException If you want to get the full response containing all data about the files, try .setFields("nextPageToken, files") . 如果要获取包含有关文件的所有数据的完整响应,请尝试.setFields("nextPageToken, files")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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