问题描述
我目前正在尝试学习如何使用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,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,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)
如果这是一个愚蠢的问题,我非常抱歉:) 在此先感谢,Thilo
解决方法
很可能找不到Animal.new('kit','Hank',7)
#=> #<Animal:0x000056fa311fd9c0 @ident="kit",@family="Hank",# @age=7,@lifespan=14,@breed_mix="Tabby">
Animal.new('kat','Boots',4)
#=> #<Animal:0x000056fa31212cf8 @ident="kat",@family="Boots",# @age=4,@lifespan=18,@breed_mix="Siamese">
资源,并且CLIENT_SECRETS
返回CreateBroadcast.class.getResourceAsStream(CLIENT_SECRETS)
。在下一行中用null
调用new InputStreamReader(in)
会得到NPE。
这与YouTube API无关。
CreateBroadcast.class.getResourceAsStream(CLIENT_SECRETS)
返回空值as the documentation states it will,因为CLIENT_SECRETS不包含指向现有资源的资源URL。
请注意,它必须是相对URL,而不是文件路径。相对网址表示在所有平台上均带有斜杠(/
)的路径。
如果您从.jar文件运行,则只需验证.jar文件是否包含您所请求的资源即可:
- 如果CLIENT_SECRETS以初始斜杠开头,则它相对于.jar文件的根目录。这意味着,如果CLIENT_SECRETS为
"/config/secrets.txt"
,则您的.jar文件需要包含一个config/secrets.txt
条目。 - 如果CLIENT_SECRETS并非以斜杠开头,则相对于CreateBroadcast类的包。这意味着,如果CLIENT_SECRETS为
"secrets.txt"
,则.jar文件需要包含一个com/google/api/services/samples/youtube/cmdline/live/secrets.txt
条目。
如果不是从.jar文件运行,则应用相同的规则,但是您需要检查运行时类路径中由IDE添加的每个目录。许多IDE将遵循Maven约定,并且仅使用项目中的target / classes子目录。
在所有情况下,如果CLIENT_SECRETS的值不是现有资源的路径,则getResourceAsStream方法将返回null。然后,您将该空值传递给new InputStreamReader
,这是导致异常的原因。