2

I found this question was marked as duplicate for one about how to prevent Null Pointer Exceptions. For clarification, the problem is that the library is throwing it. My variable isn't null. More specific help about the library is more helpful.

I am creating an app for playing music. I am trying to make use of the MediaController class to add controls to the song being played. However, when I run the .show() function, I get a Null Pointer Exception.

Here is the code for the MediaController:

public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {

    MediaController timer = view.findViewById(R.id.song_progress);
    timer.setMediaPlayer(new MediaController.MediaPlayerControl() {
        @Override
        public void start() {
            MainActivity.playingSong.start();
        }

        @Override
        public void pause() {
            MainActivity.playingSong.pause();
        }

        @Override
        public int getDuration() {
            return MainActivity.playingSong.getDuration();
        }

        @Override
        public int getCurrentPosition() {
            return MainActivity.playingSong.getCurrentPosition();
        }

        @Override
        public void seekTo(int pos) {
            MainActivity.playingSong.seekTo(pos);
        }

        @Override
        public boolean isPlaying() {
            return MainActivity.playingSong.isPlaying();
        }

        @Override
        public int getBufferPercentage() {
            return 0;
        }

        @Override
        public boolean canPause() {
            return true;
        }

        @Override
        public boolean canSeekBackward() {
            return true;
        }

        @Override
        public boolean canSeekForward() {
            return true;
        }

        @Override
        public int getAudioSessionId() {
            return MainActivity.playingSong.getAudioSessionId();
        }
    });
    timer.setAnchorView(view);
    timer.setEnabled(true);
    timer.show();

}

Here is the error log:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.luner.mobilemusic, PID: 2021
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.measure(int, int)' on a null object reference
        at android.widget.MediaController.updateFloatingWindowLayout(MediaController.java:173)
        at android.widget.MediaController.show(MediaController.java:363)
        at android.widget.MediaController.show(MediaController.java:314)
        at com.luner.mobilemusic.Playlist$Song$PlayFragment.onViewCreated(Playlist.java:271)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:892)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
        at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
        at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7156)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)

Why is this happening? Is there a good way to fix this, or should I use a different class completely? I would very much appreciate any code examples you might have.

Also, I found that removing the setAnchorView(view) prevented the error, but then caused no MediaController to appear.

Edit

After doing a more thorough search, I found two things:

  1. The line numbers aren't accurate for some reason; the line for updateFloatingWindowLayout appeared inside a different method.

  2. The culprit is the mDecor variable, which while running the setAnchorView method is set to the view variable. However, the view variable can't be null, as that would have caused an exception before getting to the show function. Thus, I still can't quite figure out the source of this ... the mDecor variable must somewhere be set to null, but only when I add a custom anchor view.

Edit 2

I have made lots of changes to the program at this point, so I am unable to test if a solution works. However, feel free to post an answer to help anyone else with the issue. I will accept any answer with a good amount of upvotes, as the upvotes signify that the solution worked. Thanks to everyone who tried to help!

mega12345mega
  • 503
  • 1
  • 6
  • 17
  • Sorry for the last comment. Maybe the view already is "showing" but it's not at the front? – Maarten Bodewes Apr 19 '20 at 23:45
  • @MaartenBodewes Are you saying that removing setAnchorView(view) is the solution? I don't know how to check if the element is showing. – mega12345mega Apr 19 '20 at 23:52
  • @chrylis -on strike- (I don't know if chrylis will get this, so hints on how to do this is helpful) I saw you closed this as a duplicate. However, yes, that is about Null Pointer Exceptions, if you read further into the question, you will find that the error comes from within the library. The MediaController variable is not null. I am trying to figure out what I did wrong for the library to complain. Flagging as duplicate isn't right if you don't actually look at the description. :) – mega12345mega Apr 20 '20 at 00:01
  • No, it looks like you're passing a null `View` object to `onViewCreated` somehow. – chrylis -cautiouslyoptimistic- Apr 20 '20 at 00:11
  • @chrylis-onstrike- I see what you are saying. After looking more closely, that is what appears to be happening. However, that isn't what is happening. It must be a view variable inside the library, since I run methods of my view variable before sending it to the MediaController library. They would have sent out their own Null Pointer Exceptions first. – mega12345mega Apr 20 '20 at 00:14

0 Answers0