HaarigerHarald / android-youtubeExtractor

Deprecated: Android based YouTube URL extractor and downloader
Other
877 stars 304 forks source link

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String at.huber.youtubeExtractor.YtFile.getUrl()' on a null object reference #214

Open ldev1211 opened 2 years ago

ldev1211 commented 2 years ago

In the first install application in my physic device. I really can download video from youtube to my device's storage. But the next time, my application crashed immediately. Then Android Studio IDE announce to me error in line init String downloadURL like title of this question. This is my code:

String youtubeLink = ("https://www.youtube.com/watch?v=" + videoYT.getId().getVideoId());
                    YouTubeUriExtractor ytEx = new YouTubeUriExtractor(context) {
                        @Override
                        public void onUrisAvailable(String videoId, String videoTitle, SparseArray<YtFile> ytFiles) {
                            if (ytFiles != null) {
                                int itag = 22;
                                String downloadURL = ytFiles.get(itag).getUrl();// line where Android Studio IDE gives an error
                                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downloadURL));
                                String title = URLUtil.guessFileName(downloadURL,null,null);
                                request.setTitle(title);
                                request.setDescription("Downloading file...");
                                String cookie = CookieManager.getInstance().getCookie(downloadURL);
                                request.addRequestHeader("cookie",cookie);
                                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,title);
                                DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
                                downloadManager.enqueue(request);
                                Toast.makeText(context, "Bắt đầu tải xuống...", Toast.LENGTH_SHORT).show();
                            }
                        }
                    };
                    ytEx.execute(youtubeLink);
masaneo commented 2 years ago

I don't know if u already solved this problem but i had similar problem. For me worked reading what's inside ytFiles array and then I noticed that there's no element with itag = 22 (there was 18, 133, 134, 135) which probably might vary between different videos from youtube.