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

标签 java nullpointerexception google-drive-api

配置并成功测试 https://developers.google.com/drive/v3/web/quickstart/java 中的快速入门代码后

尝试其他文件方法后,例如 getFileExtension()、get WebViewLink()、get Md5 Checksum(),即使 file.getName() 和 file.getId() 工作正常,我也会收到 NullPointerException。

原始工作代码:

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

如果您能帮助我理解在文件对象上调用其他方法的空指针原因,我将不胜感激。 非常感谢

最佳答案

您需要将所需的所有字段放入.setFields("nextPageToken, files(id, name)")。现在,您只有 idname。因此,您收到的部分响应只有 idname,因此其他所有内容都是 null,因此您会收到 NullPointerException 。如果您想获取包含有关文件的所有数据的完整响应,请尝试 .setFields("nextPageToken, files")

关于java - 文件对象方法抛出 NullPointerException - Google Drive API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41475427/

相关文章:

java - servlet 覆盖 HttpServlet 服务方法的正当理由

java - 无法解析 javax.media.nativewindow.Capability 类型。它是从所需的 .class 文件间接引用的

java - Camel Direct 端点异常

java - 属性文件不会在静态代码中加载,出现 NullPointerException

java - JEdi​​torPane.getPreferredSize 并不总是在 Java 9 中工作?

java - 使用 TestNG 运行代码时出现空指针异常

android - 在相机设置参数上获取空指针异常

google-apps-script - 在其他文档上设置属性

javascript - 为什么我在尝试从我的 Electron 应用程序调用 Google Drive Api 时收到 'Daily Limit for Unauthenticated Use Exceeded' 错误?

javascript - 为什么 Javascript 的 OAuth 不需要客户端密码?