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:
- The line numbers aren't accurate for some reason; the line for - updateFloatingWindowLayoutappeared inside a different method.
- The culprit is the - mDecorvariable, which while running the- setAnchorViewmethod 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- showfunction. 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!
