Skip to content
Advertisement

Attempt to invoke virtual method on Playerview.setplayer on a null object reference

I am getting a null pointer exception where there shouldnt be one.

Here is the error readout

 java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.exoplayer2.ui.PlayerView.setPlayer(com.google.android.exoplayer2.Player)' on a null object reference
        at com.example.corbettreportpodcasts.PodcastContentActivity.initializePlayer(PodcastContentActivity.java:40)
        at com.example.corbettreportpodcasts.PodcastContentActivity.onStart(PodcastContentActivity.java:55)

Here is the activity source code (Its from a google codelab on the exoplayer) and has been modified a little. The layout file is just a Playerview element with its ID as video_view

package com.example.corbettreportpodcasts;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.util.Util;

import androidx.appcompat.app.AppCompatActivity;

public class PodcastContentActivity extends AppCompatActivity implements Player.EventListener {

    PlayerView playerView;
    private SimpleExoPlayer player;

    private boolean playWhenReady = true;
    private int currentWindow = 0;
    private long playbackPosition = 0;
    private String source;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        playerView = findViewById(R.id.video_view);

        Intent intent = getIntent();
        source = intent.getExtras().getString("PODCAST_SOURCE");

    }

    private void initializePlayer() {
        player = new SimpleExoPlayer.Builder(this).build();
        playerView.setPlayer(player); // HERE IS THE ERROR

        MediaItem mediaItem = MediaItem.fromUri(source);
        player.setMediaItem(mediaItem);

        player.setPlayWhenReady(playWhenReady);
        player.seekTo(currentWindow, playbackPosition);
        player.prepare();

    }

    @Override
    public void onStart() {
        super.onStart();
        if (Util.SDK_INT >= 24) {
            initializePlayer();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        hideSystemUi();
        if ((Util.SDK_INT < 24 || player == null)) {
            initializePlayer();
        }
    }

    @SuppressLint("InlinedApi")
    private void hideSystemUi() {
        playerView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
    }

    @Override
    public void onPause() {
        super.onPause();
        if (Util.SDK_INT < 24) {
            releasePlayer();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (Util.SDK_INT >= 24) {
            releasePlayer();
        }
    }


    private void releasePlayer() {
        if (player != null) {
            playWhenReady = player.getPlayWhenReady();
            playbackPosition = player.getCurrentPosition();
            currentWindow = player.getCurrentWindowIndex();
            player.release();
            player = null;
        }
    }
}

Any help is appreciated. This is the codelab HERE

The following is the xml file

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/video_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


    </com.google.android.exoplayer2.ui.PlayerView>

</androidx.constraintlayout.widget.ConstraintLayout>

Advertisement

Answer

I called setContentView on the wrong layout file leading to findviewbyid giving me a null pointer

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement