I am getting a NullPointerException Error while trying to play a Downloaded Content. I'm using a PlayerActivity class to play any content in my app. The content is being fetched using a reference number thus a getReference() method. The get reference is being fetched from a Content.class that points to the reference of any content in my API. So basically it fetches the content and plays the video in the Player Activity which is below, but the downloaded content isn't playing and when you try to play it crashes the app, then throws the Null Pointer error.
private Content content;
private Playback playbackObj;
private User user;
private PlaybackViewModel plvm;
private DefaultBandwidthMeter.Builder BANDWIDTH_METER;
private SimpleExoPlayer player;
private StyledPlayerView playerView;
int CURRENT_WINDOW;
boolean IS_STREAM = true;
long PLAYBACK_POSITION = 0;
AlertDialog alertDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
Bundle extras = getIntent().getExtras();
if(extras != null){
content = (Content) extras.getSerializable(Content.LOCAL_CONTENT_OBJECT);
IS_STREAM = extras.getBoolean(Content.LOCAL_IS_STREAM);
}
plvm = new ViewModelProvider(this).get(PlaybackViewModel.class);
playbackObj = plvm.getByRef(content.getReference());
playbackObj = playbackObj != null ? playbackObj : new Playback();
PLAYBACK_POSITION = playbackObj.getTimeElapsed() * 1000;
BANDWIDTH_METER = new DefaultBandwidthMeter.Builder(PlayerActivity.this);
apiService = new APIService(this);
downloadUtils = new DownloadUtils(this);
user = new User(this);
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(content.getTitle());
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
playerView = findViewById(R.id.playerView);
playerView.setControllerVisibilityListener(new StyledPlayerControlView.VisibilityListener() {
@Override
public void onVisibilityChange(int visibility) {
if(getSupportActionBar() != null){
if(visibility == 0){
getSupportActionBar().show();
}
else{
getSupportActionBar().hide();
}
}
}
});
// Fullscreen view
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Prevent screen from going off during playback
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// Prevent screenshots
SecurityUtils.preventScreenshot(this);
alertDialog = Utils.getSimpleDialog(this, "");
hideSystemUi();
mCastContext = CastContext.getSharedInstance(this);
mCastSession = mCastContext.getSessionManager().getCurrentCastSession();
initPlayer();
}
My error points here playbackObj = plvm.getByRef(content.getReference());
Here is the error thrown
**E/AndroidRuntime: FATAL EXCEPTION: main
Process: org.mychoicetv.app, PID: 28228
java.lang.RuntimeException: Unable to start activity ComponentInfo{org.mychoicetv.app/org.mychoicetv.app.playback.PlayerActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.mychoicetv.app.library.models.Content.getReference()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3303)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3411)
at android.app.ActivityThread.-wrap12(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1994)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7529)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.mychoicetv.app.library.models.Content.getReference()' on a null object reference
at org.mychoicetv.app.playback.PlayerActivity.onCreate(PlayerActivity.java:145)
at android.app.Activity.performCreate(Activity.java:7383)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)**