Youtube 中的 NullPointerExceptions API

NullPointerExceptions in Youtube API

我目前正在尝试学习如何使用 Youtube API 和其他东西,但我遇到了一些 NullPointerException 错误,我只是想不通为什么...

我已经尝试了文档中的所有内容,但没有任何改变。

private static final String APPLICATION_NAME = "API code samples";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

/**
 * Create an authorized Credential object.
 *
 * @return an authorized Credential object.
 * @throws IOException
 */
public static Credential authorize(final NetHttpTransport httpTransport) throws IOException {
    // Load client secrets.
    InputStream in = CreateBroadcast.class.getResourceAsStream(CLIENT_SECRETS);
    GoogleClientSecrets clientSecrets =
      GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
    // Build flow and trigger user authorization request.
    GoogleAuthorizationCodeFlow flow =
        new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY, clientSecrets, SCOPES)
        .build();
    Credential credential =
        new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
    return credential;
}

/**
 * Build and return an authorized API client service.
 *
 * @return an authorized API client service
 * @throws GeneralSecurityException, IOException
 */
public static YouTube getService() throws GeneralSecurityException, IOException {
    final NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    Credential credential = authorize(httpTransport);
    return new YouTube.Builder(httpTransport, JSON_FACTORY, credential)
        .setApplicationName(APPLICATION_NAME)
        .build();
}

/**
 * Call function to create API service object. Define and
 * execute API request. Print API response.
 *
 * @throws GeneralSecurityException, IOException, GoogleJsonResponseException
 */
public static void main(String[] args)
    throws GeneralSecurityException, IOException, GoogleJsonResponseException {
    YouTube youtubeService = getService();
    
    // Define the LiveBroadcast object, which will be uploaded as the request body.
    LiveBroadcast liveBroadcast = new LiveBroadcast();
    
    // Add the contentDetails object property to the LiveBroadcast object.
    LiveBroadcastContentDetails contentDetails = new LiveBroadcastContentDetails();
    contentDetails.setEnableClosedCaptions(true);
    contentDetails.setEnableContentEncryption(true);
    contentDetails.setEnableDvr(true);
    contentDetails.setEnableEmbed(true);
    contentDetails.setRecordFromStart(true);
    contentDetails.setStartWithSlate(true);
    liveBroadcast.setContentDetails(contentDetails);
    
    // Add the snippet object property to the LiveBroadcast object.
    LiveBroadcastSnippet snippet = new LiveBroadcastSnippet();
    snippet.setScheduledStartTime(new DateTime("2024-01-30T00:00:00.000Z"));
    snippet.setTitle("Test broadcast");
    liveBroadcast.setSnippet(snippet);
    
    // Add the status object property to the LiveBroadcast object.
    LiveBroadcastStatus status = new LiveBroadcastStatus();
    status.setPrivacyStatus("unlisted");
    liveBroadcast.setStatus(status);

    // Define and execute the API request
    YouTube.LiveBroadcasts.Insert request = youtubeService.liveBroadcasts()
        .insert("snippet,contentDetails,status", liveBroadcast);
    LiveBroadcast response = request.execute();
    System.out.println(response);
}

这些是我不断收到的错误:

Exception in thread "main" java.lang.NullPointerException
at java.base/java.io.Reader.<init>(Reader.java:167)
at java.base/java.io.InputStreamReader.<init>(InputStreamReader.java:72)
at com.google.api.services.samples.youtube.cmdline.live.CreateBroadcast.authorize(CreateBroadcast.java:47)
at com.google.api.services.samples.youtube.cmdline.live.CreateBroadcast.getService(CreateBroadcast.java:65)
at com.google.api.services.samples.youtube.cmdline.live.CreateBroadcast.main(CreateBroadcast.java:79)

如果这是一个愚蠢的问题,我很抱歉:) 提前致谢, 蒂洛

很可能找不到 CLIENT_SECRETS 资源并且 CreateBroadcast.class.getResourceAsStream(CLIENT_SECRETS) returns null。在下一行用 null 调用 new InputStreamReader(in) 以 NPE 结束。

这与 YouTube 无关 API。

CreateBroadcast.class.getResourceAsStream(CLIENT_SECRETS) 返回 null,as the documentation states it will,因为 CLIENT_SECRETS 不包含指向现有资源的资源 URL。

请注意,它必须是一个相对路径URL,而不是文件路径。在所有平台上,相对 URL 表示带有正斜杠 (/) 的路径。

如果您 运行 来自 .jar 文件,您可以简单地验证 .jar 文件是否包含您请求的资源:

  • 如果 CLIENT_SECRETS 以初始斜杠开头,则它是相对于 .jar 文件的根目录的。意思是,如果 CLIENT_SECRETS 是 "/config/secrets.txt",您的 .jar 文件需要包含一个 config/secrets.txt 条目。
  • 如果CLIENT_SECRETS不是以斜杠开头,它是相对于CreateBroadcastclass的包。意思是,如果 CLIENT_SECRETS 是 "secrets.txt",.jar 文件需要包含一个 com/google/api/services/samples/youtube/cmdline/live/secrets.txt 条目。

如果您不是 运行 来自 .jar 文件,则适用相同的规则,但您需要检查运行时 class路径中的每个目录,这些目录是由您的 [=47= 添加的].许多 IDE 将符合 Maven 约定,并且只会使用项目中的 target/classes 子目录。

在所有情况下,如果 CLIENT_SECRETS 的值不是现有资源的路径,则 getResourceAsStream 方法 returns 为 null。然后您将该空值传递给 new InputStreamReader,这就是您的异常的原因。