1

I'm using the youtube-api, the video upload sample works fine when I set the video_path string to "/video_name.mp4" placed under the workspace .

private static String video_path = "/video_name.MP4";

But once I set it to the absolute path private static String video_path = "C:/Users/Ip300/Desktop/video_name.MP4";

I get the error

" Throwable: null java.lang.NullPointerException..."

like the video doesn't exist. PS : I tested the path on windows it redirects correctly to the video.

the full code is :

public class UploadVideo {

private static YouTube youtube;
private static final String VIDEO_FILE_FORMAT = "video/*";
private static final String SAMPLE_VIDEO_FILENAME = "video_name.mp4";
private static  String video_path = "C:/Users/Ip300/Desktop/video_name.MP4";

public static void main(String[] args) {

    List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube.upload");
    try {     
        Credential credential = Auth.authorize(scopes, "uploadvideo");
        youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName(
                "youtube-cmdline-uploadvideo-sample").build();
        System.out.println("Uploading: " + SAMPLE_VIDEO_FILENAME);
        Video videoObjectDefiningMetadata = new Video();
        VideoStatus status = new VideoStatus();
        status.setPrivacyStatus("public");
        videoObjectDefiningMetadata.setStatus(status); 
        VideoSnippet snippet = new VideoSnippet();           
        Calendar cal = Calendar.getInstance();
        snippet.setTitle("Test Upload via Java on " + cal.getTime());
        snippet.setDescription(
                "Video uploaded via YouTube Data API V3 using the Java library " + "on " + cal.getTime());
        // Set the keyword tags that you want to associate with the video.
        List<String> tags = new ArrayList<String>();
        tags.add("test");
        tags.add("example");
        tags.add("java");
        tags.add("YouTube Data API V3");
        tags.add("erase me");
        snippet.setTags(tags);
        videoObjectDefiningMetadata.setSnippet(snippet);
        InputStreamContent mediaContent = new InputStreamContent(VIDEO_FILE_FORMAT,
                UploadVideo.class.getResourceAsStream(video_path));
        YouTube.Videos.Insert videoInsert = youtube.videos()
                .insert("snippet,statistics,status", videoObjectDefiningMetadata, mediaContent);
        MediaHttpUploader uploader = videoInsert.getMediaHttpUploader();
        uploader.setDirectUploadEnabled(false);
        MediaHttpUploaderProgressListener progressListener = new MediaHttpUploaderProgressListener() {
            public void progressChanged(MediaHttpUploader uploader) throws IOException {
                switch (uploader.getUploadState()) {
                    case INITIATION_STARTED:
                        System.out.println("Initiation Started");
                        break;
                    case INITIATION_COMPLETE:
                        System.out.println("Initiation Completed");
                        break;
                    case MEDIA_IN_PROGRESS:
                        System.out.println("Upload in progress");
                        System.out.println("Upload percentage: " + uploader.getProgress());
                        break;
                    case MEDIA_COMPLETE:
                        System.out.println("Upload Completed!");
                        break;
                    case NOT_STARTED:
                        System.out.println("Upload Not Started!");
                        break;
                }
            }
        };
        uploader.setProgressListener(progressListener);
        System.out.println("\n================== Returned Video ==================\n");
        System.out.println("  - Id: " + returnedVideo.getId());
        System.out.println("  - Title: " + returnedVideo.getSnippet().getTitle());
        System.out.println("  - Tags: " + returnedVideo.getSnippet().getTags());
        System.out.println("  - Privacy Status: " + returnedVideo.getStatus().getPrivacyStatus());
        System.out.println("  - Video Count: " + returnedVideo.getStatistics().getViewCount());
    } catch (GoogleJsonResponseException e) {
        System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode() + " : "
                + e.getDetails().getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("IOException: " + e.getMessage());
        e.printStackTrace();
    } catch (Throwable t) {
        System.err.println("Throwable: " + t.getMessage());
        t.printStackTrace();
    }
}
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
kingabdr
  • 588
  • 5
  • 12
  • I'm not sure how we're supposed to help without any code. – tnw Apr 05 '17 at 18:59
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – tnw Apr 05 '17 at 19:00

1 Answers1

0

Class.getResourceAsStream() expects a leading ' / ' in order to interpret the path as an absoute path. Without it the path is supposed to be a relative one. Whereas ClassLoader.getResourceAsStream() uses only absolute paths, so there is no need for a leading ' / '.

Both return null in case the resource was not found.

You use the UploadVideo.class.getResourceAsStream() method to initialize the YouTube.Videos.Insert videoInsert.

For further information: http://www.javaworld.com/article/2077352/java-se/smartly-load-your-properties.html

Note: If you use a native path and work on a Windows machine, the proper file seperator is ' \ ' instead of ' / '.

However for native paths ( e.g. c:\path\file ) there is no need for getResourceAsStream(). You can simply use new FileInputStream("c:\path\file");

Don Foumare
  • 444
  • 3
  • 13
  • I aim to get an absolute path from the fileChooser and then pass it as the video_path variable. the output of the absolute path is "C:/Users/Ip300/Desktop/video_name.MP4" which is not working even with a leading '/' – kingabdr Apr 05 '17 at 19:55
  • @kingabdr sorry.. there was a misconception with relative, absolute and native path.. i updated my answer. – Don Foumare Apr 05 '17 at 20:32