Video Player Null Pointer Exception using Google Media Framework Exoplayer
Asked Answered
J

2

1

I am using GMF sample to play Video in my app, where I am fetching video title and url from live JSON, here is the sample of my JSON :

{
"videos": [ 
{
"title":"Video 1",
"url":"88.mp4"
},
{
"title":"Video 2",
"url":"l5.mp4"
}
]}

And this is what my code looks like :

MainActivity.java:

  JSONObject jsono = new JSONObject(data);
  JSONArray jarray = jsono.getJSONArray("videos");

  for (int i = 0; i < jarray.length(); i++) {
       JSONObject object = jarray.getJSONObject(i);

       Videos video = new Videos();

       video.setTitle(object.getString("title"));
       video.setUrl(object.getString("url"));

       videosArrayList.add(video);
   }

Whenever, I do tap on any of the list item to play Video, getting NPE, see complete Log:

03-17 13:14:34.667 18799-18799/com.google.googlemediaframeworkdemo.demo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.google.googlemediaframeworkdemo.demo, PID: 18799
java.lang.NullPointerException
at com.google.android.libraries.mediaframework.exoplayerextensions.RendererBuilderFactory.createRendererBuilder(RendererBuilderFactory.java:34)
at com.google.android.libraries.mediaframework.layeredvideo.LayerManager.<init>(LayerManager.java:78)
at com.google.android.libraries.mediaframework.layeredvideo.SimpleVideoPlayer.<init>(SimpleVideoPlayer.java:112)
at com.google.android.libraries.mediaframework.layeredvideo.SimpleVideoPlayer.<init>(SimpleVideoPlayer.java:81)
at com.google.googlemediaframeworkdemo.demo.adplayer.ImaPlayer.<init>(ImaPlayer.java:388)
at com.google.googlemediaframeworkdemo.demo.adplayer.ImaPlayer.<init>(ImaPlayer.java:434)
at com.google.googlemediaframeworkdemo.demo.adplayer.ImaPlayer.<init>(ImaPlayer.java:467)
at com.google.googlemediaframeworkdemo.demo.MainActivity$1.onItemClick(MainActivity.java:81)
at android.widget.AdapterView.performItemClick(AdapterView.java:299)
at android.widget.AbsListView.performItemClick(AbsListView.java:1152)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3014)
at android.widget.AbsListView$3.run(AbsListView.java:3865)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
03-17 13:14:34.803 18799-18861/com.google.googlemediaframeworkdemo.demo D/dalvikvm: threadid=17: interp stack at 0x56eb0000
03-17 13:14:34.803 18799-18861/com.google.googlemediaframeworkdemo.demo D/dalvikvm: init ref table
03-17 13:14:34.803 18799-18861/com.google.googlemediaframeworkdemo.demo D/dalvikvm: init mutex
03-17 13:14:34.903 18799-18861/com.google.googlemediaframeworkdemo.demo D/MediatekClassFactory: createInstance(): Begin = 9861250
03-17 13:14:34.904 18799-18861/com.google.googlemediaframeworkdemo.demo D/MediatekClassFactory: create Instance with :  interface com.mediatek.common.telephony.IOnlyOwnerSimSupport
03-17 13:14:34.912 18799-18861/com.google.googlemediaframeworkdemo.demo W/MediatekClassFactory: Tablet not exist!, Get obj from default class
03-17 13:14:34.916 18799-18861/com.google.googlemediaframeworkdemo.demo D/MediatekClassFactory: create Instance from tablet library :  com.mediatek.tb.telephony.OnlyOwnerSimSupport
03-17 13:14:34.920 18799-18861/com.google.googlemediaframeworkdemo.demo D/MediatekClassFactory: createInstance(): End = 9861267

I wrote code as I think it should be, so may I know where I am missing ? what I am missing ? and how can i resolve it ?

Jordanna answered 17/3, 2016 at 9:11 Comment(0)
B
1

In this part of the code

Videos video = new Videos();

video.setTitle(object.getString("title"));
video.setUrl(object.getString("url"));

videosArrayList.add(video);

you never assign the video field in Videos object video ( never call public void setVideo(Video video) { this.video = video; }). You use the default constructor public Videos() { } which initializes the field video to null

To solve this issue add this line

video.setVideo(new Video(object.getString("url"),VideoType.MP4)))
Baltic answered 17/3, 2016 at 10:41 Comment(3)
I explained exactly the same to you Sophie, but may be you didn't understand my answer. Have a nice day :)Stiffnecked
what if I would like to play Video by default when I do tap on List Item, like still I do tap on List Item > then I have to click on Play button in Video Player... now I want whenever user do tap on List Item then play video don't need to show Play button to play video...Jordanna
Aren't you doing that already here public void onItemClick(... imaPlayer.play(); ... ?Baltic
S
2

When you fetch the response in your doInBackgroud method you create Videos objects and put title and url in its, but when you create your player you try get Video object when call videosArrayList.get(i).getVideo(), but it is never setted. In that point is your NullPointerException.

Stiffnecked answered 17/3, 2016 at 9:38 Comment(1)
yes I did not understand your answer, but anyways I ticked your answer as useful... thank youJordanna
B
1

In this part of the code

Videos video = new Videos();

video.setTitle(object.getString("title"));
video.setUrl(object.getString("url"));

videosArrayList.add(video);

you never assign the video field in Videos object video ( never call public void setVideo(Video video) { this.video = video; }). You use the default constructor public Videos() { } which initializes the field video to null

To solve this issue add this line

video.setVideo(new Video(object.getString("url"),VideoType.MP4)))
Baltic answered 17/3, 2016 at 10:41 Comment(3)
I explained exactly the same to you Sophie, but may be you didn't understand my answer. Have a nice day :)Stiffnecked
what if I would like to play Video by default when I do tap on List Item, like still I do tap on List Item > then I have to click on Play button in Video Player... now I want whenever user do tap on List Item then play video don't need to show Play button to play video...Jordanna
Aren't you doing that already here public void onItemClick(... imaPlayer.play(); ... ?Baltic

© 2022 - 2024 — McMap. All rights reserved.