Home > Software engineering >  How to resolve NullPointerException on HttpURLConnection?
How to resolve NullPointerException on HttpURLConnection?

Time:02-28

I am developing an Android application, using ExoPlayer for video playback, Glide for image display, and a download library for file download, and they all have NullPointerException related to HttpURLConnection, I'm sure the link used is available.

The magic is, I just follow these library's The method described in the official documentation loads the data normally, and this error occurs from time to time, and the three libraries may not throw NullPointerException at the same time, and I don't even know how to reproduce this problem.

This is the error thrown by ExoPlayer, which prevents the video from loading:

E/LoadTask: Unexpected exception loading stream
      java.lang.NullPointerException: wrapped.getHeaderField(key) must not be null
        at com.android.tools.appinspection.network.httpurl.TrackedHttpURLConnection.getHeaderField(TrackedHttpURLConnection.kt:317)
        at com.android.tools.appinspection.network.httpurl.HttpsURLConnectionWrapper.getHeaderField(HttpsURLConnectionWrapper.kt:182)
        at com.google.android.exoplayer2.upstream.DefaultHttpDataSource.isCompressed(DefaultHttpDataSource.java:829)
        at com.google.android.exoplayer2.upstream.DefaultHttpDataSource.open(DefaultHttpDataSource.java:413)
        at com.google.android.exoplayer2.upstream.StatsDataSource.open(StatsDataSource.java:84)
        at com.google.android.exoplayer2.source.ProgressiveMediaPeriod$ExtractingLoadable.load(ProgressiveMediaPeriod.java:1009)
        at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:412)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:923)

The video playback part of the code is as follows:

            val factory = ProgressiveMediaSource.Factory(
                DefaultHttpDataSource.Factory()
            )
            val mediaSource = arrayListOf<MediaSource>(
                factory.createMediaSource(MediaItem.fromUri(video.base_url)),
                factory.createMediaSource(MediaItem.fromUri(audio.base_url))
            )
            val media = MergingMediaSource(true, *mediaSource.toTypedArray())
            ViewModel.getPlayer()?.let { player ->
                player.setMediaSource(media)
                player.prepare()
            }

And this is the error thrown by Aria, which cause software to crash:

E/AndroidRuntime: FATAL EXCEPTION: pool-5-thread-2
    Process: io.github.sgpublic.bilidownload, PID: 22400
    java.lang.NullPointerException: wrapped.getRequestProperty(field) must not be null
        at com.android.tools.appinspection.network.httpurl.TrackedHttpURLConnection.getRequestProperty(TrackedHttpURLConnection.kt:235)
        at com.android.tools.appinspection.network.httpurl.HttpsURLConnectionWrapper.getRequestProperty(HttpsURLConnectionWrapper.kt:274)
        at com.arialyy.aria.http.ConnectionHelp.setConnectParam(ConnectionHelp.java:146)
        at com.arialyy.aria.http.download.HttpDFileInfoTask.run(HttpDFileInfoTask.java:82)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:923)

I have tried Invalidate and Restart, and even uninstalled and reinstalled Android Studio completely, but the problem still cannot be solved, so what is the problem?

CodePudding user response:

dear use this Library for play youtube videos , its amazing and easy to use and manage , this is the best way to play youtube videos in your application

    implementation 'com.pierfrancescosoffritti.androidyoutubeplayer:chromecast-sender:0.23'

Manifest (if you want to use full screen video play)

  <activity
   android:name="com.pksofterltd.videoislamapp.Activities.LiveVideo"
   android:exported="true"
       android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize|uiMode">

        </activity>

XML

<com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
        android:id="@ id/youtube_player_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:showYouTubeButton="false" /

JAVA

  YouTubePlayerView youTubePlayerView;
    YouTubePlayer youTubePlr;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        youTubePlayerView = findViewById(R.id.youtube_player_view);

// link of video https://www.youtube.com/watch?v=HoJtAI0B-Bw
// you just use video ID insted on complete URL .video id you find at the end of url
// here is video ID is "HoJtAI0B-Bw" 

        play("HoJtAI0B-Bw");


}
public void play(String url){
        getLifecycle().addObserver(youTubePlayerView);


        youTubePlayerView.addFullScreenListener(new YouTubePlayerFullScreenListener() {
            @Override
            public void onYouTubePlayerEnterFullScreen() {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
            }

            @Override
            public void onYouTubePlayerExitFullScreen() {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);

            }
        });
        youTubePlayerView.addYouTubePlayerListener(new AbstractYouTubePlayerListener() {
            @Override
            public void onReady(@NonNull YouTubePlayer youTubePlayer) {
                youTubePlr =youTubePlayer;
                
                    youTubePlr.loadVideo(url,0);
                





            }
        });
    }

CodePudding user response:

I found the related entry, and to sum it up this is a bug in the TrackedHttpURLConnection implementation, the fix will be capped around mid-late March.

  • Related