Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8674323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:40:56+00:00 2026-06-12T19:40:56+00:00

I keep getting null pointer exceptions for methods of MediaPlayer. I was finally able

  • 0

I keep getting null pointer exceptions for methods of MediaPlayer. I was finally able to get the play function to work by moving the code for play and initialize of play functions into a separate method and call that method from inside the onClick listener.

However am still getting null pointer exception for the apps pause function. I am using pause method of media player. How to the get pause to work? I think the problem is somewhere in the structure of my code and how it is organized.

I tried moving the initialization of the Media player to a different place in the code. and and nothing seems to work. Any ideas?

// onclick listener for the playing the selected song
playB.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        playSong();
    }
});

// onclick listener for pausing the song that is playing
pauseB.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        pauseSong();
    }
});

// method to pause song
public void pauseSong(){
    player.pause();
    length = player.getCurrentPosition();
}

// method to play song and initialize the MediaPlayer class with one file
// from the drawable folder, need to initialize with something or it will be null
// for that i decided to use an mp3 in R.raw folder
public void  playSong(){
    // Play song
    MediaPlayer player = MediaPlayer.create(this, R.raw.g2);
    player.reset();
    try {
        player.setDataSource(selectedAudioPath);

        player.prepare();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    player.seekTo(length);
    player.start();
} // play method
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:40:58+00:00Added an answer on June 12, 2026 at 7:40 pm

    You have to make a MediaPlayer player global variable. player is not visible for the pauseSong() method therefore you have nullPointerException. Create a MediaPlayer player in your main class and then in onPlaySong() initialize it only like this:

    player = MediaPlayer.create(this, R.raw.g2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to access servletContextin a controller like so, but keep getting null pointer
so i am using the Processing IDE and keep getting this strange null pointer
I am getting a null pointer exception before I can even get to my
I keep getting a null exception at the ; below. The ApiUsername & ApiPassword
I keep getting this error with my Play 2.0 application: play.core.ActionInvoker$$anonfun$receive$1$$anon$1: Execution exception [[MatchError:
i keep getting a sytax error when trying to add (function (e) { e.preventDefault;
Im working on this bit of code and I keep getting a segmentation fault.
I want to copy values into a struct using a pointer. I keep getting
Summary: Keep getting null response despite public data and setting callback to enable cross
I am fairly new to C++ and I keep getting segmentation fault with pointer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.