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 8492627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:46:26+00:00 2026-06-10T22:46:26+00:00

I have a problem with Android media player, I’m working on some little quiz,

  • 0

I have a problem with Android media player, I’m working on some little quiz, and I have method correctAnswer(), where I create MediaPlayer with some sound, then prepare it, and then start it, but I get this NullPointerException error.

I am trying to play mp3 sound.

Here is that method correctAnswer():

    private void correctAnswer() {
    MediaPlayer correctSound = MediaPlayer.create(this, R.raw.correct);
    try {
        correctSound.prepare();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    correctSound.start();

    Toast.makeText(getApplicationContext(), "Correct answer +1",
            Toast.LENGTH_SHORT).show();
    progressBar.incrementProgressBy(1);
    tvProgress.setText(progressBar.getProgress() + "/20");
    generateQuestion();
    endGame();
}

Here is LogCat:

09-04 21:21:30.546: E/AndroidRuntime(513): Uncaught handler: thread main exiting due to         uncaught exception
09-04 21:21:30.567: E/AndroidRuntime(513): java.lang.NullPointerException
09-04 21:21:30.567: E/AndroidRuntime(513):  at   com.example.americansportsquiz.PlayStageActivity.correctAnswer(PlayStageActivity.java:121)
09-04 21:21:30.567: E/AndroidRuntime(513):  at com.example.americansportsquiz.PlayStageActivity.onClick(PlayStageActivity.java:75)
09-04 21:21:30.567: E/AndroidRuntime(513):  at android.view.View.performClick(View.java:2364)
09-04 21:21:30.567: E/AndroidRuntime(513):  at android.view.View.onTouchEvent(View.java:4179)
09-04 21:21:30.567: E/AndroidRuntime(513):  at android.widget.TextView.onTouchEvent(TextView.java:6541)
09-04 21:21:30.567: E/AndroidRuntime(513):  at android.view.View.dispatchTouchEvent(View.java:3709)
09-04 21:21:30.567: E/AndroidRuntime(513):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
09-04 21:21:30.567: E/AndroidRuntime(513):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
09-04 21:21:30.567: E/AndroidRuntime(513):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
09-04 21:21:30.567: E/AndroidRuntime(513):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
09-04 21:21:30.567: E/AndroidRuntime(513):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
09-04 21:21:30.567: E/AndroidRuntime(513):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
09-04 21:21:30.567: E/AndroidRuntime(513):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
09-04 21:21:30.567: E/AndroidRuntime(513):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
09-04 21:21:30.567: E/AndroidRuntime(513):  at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
09-04 21:21:30.567: E/AndroidRuntime(513):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
09-04 21:21:30.567: E/AndroidRuntime(513):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
09-04 21:21:30.567: E/AndroidRuntime(513):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 21:21:30.567: E/AndroidRuntime(513):  at android.os.Looper.loop(Looper.java:123)
09-04 21:21:30.567: E/AndroidRuntime(513):  at android.app.ActivityThread.main(ActivityThread.java:4363)
09-04 21:21:30.567: E/AndroidRuntime(513):  at java.lang.reflect.Method.invokeNative(Native Method)
09-04 21:21:30.567: E/AndroidRuntime(513):  at java.lang.reflect.Method.invoke(Method.java:521)
09-04 21:21:30.567: E/AndroidRuntime(513):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
09-04 21:21:30.567: E/AndroidRuntime(513):  at    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
09-04 21:21:30.567: E/AndroidRuntime(513):  at dalvik.system.NativeStart.main(Native Method)

EDIT As I said, this is quiz app, and it play sound for 7 correct answers, but it fails on 8, and give me this null pointer expcetion. Before it fails at 8 correct answer it give me all time this warning.

09-04 21:41:33.086: W/MediaPlayer(598): info/warning (1, 44)
  • 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-10T22:46:28+00:00Added an answer on June 10, 2026 at 10:46 pm

    It looks like your are not releasing the previous MediaPlayer before you are trying to create a new one. Simply keep a reference to this MediaPlayer and replay the sound, rather than creating a new copy every time:

    MediaPlayer correctSound;
    
    private void correctAnswer() {
        if(correctSound == null)
            correctSound = MediaPlayer.create(this, R.raw.correct);
    
            if(correctSound == null) {
               // Something failed... try again or break out of this method
            }
    
            try {
                correctSound.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        correctSound.start();
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with android 4.0.3. I'm using the method below to get local
I have some problem with Android AsyncTask. There is an Activity which contains some
I have an android app which runs mediaplayer.It works fine my problem is that
I have one problem with android calendar programming, so If there are 2 months
I have a problem regarding Android App. I have created an application that download
I have a problem with the android alarmmanager. I'm setting the alarmmanager with two
I have a problem in my android application, I have a hand page, I
I have a problem with my android application. That application which uses the kSOAP
i have a problem with my Android Application. The Error occures when i want
I have a problem on an Android app. I have 2 activities : Activity1

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.