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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:45:33+00:00 2026-06-10T04:45:33+00:00

I am using media player to play a mp3 file.But when i run it,i

  • 0

I am using media player to play a mp3 file.But when i run it,i am getting the null pointer exception.Please tell me what is the problem.

This is the code:

package com.example.soundplayer;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

    /**
     * Variables
     */
    MediaPlayer mp = null;
    String hello = "Hello!";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /**
         * Talking with the buttonHello
         */
        final Button buttonHello = (Button) findViewById(R.id.idHello);
        buttonHello.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                managerOfSound(hello);
            } // END onClick()
        }); // END buttonHello
    } // END onCreate()

    /**
     * Manager of Sounds
     */
    protected void managerOfSound(String theText) {
        if (mp != null) {
            mp.reset();
            mp.release();
        }
        if (theText == hello){
            mp = new MediaPlayer();
            mp = MediaPlayer.create(MainActivity.this, R.raw.sound);
            mp.start();}
    }
}

This is the exception:

08-22 13:13:01.001: W/dalvikvm(785): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
08-22 13:13:01.001: E/AndroidRuntime(785): Uncaught handler: thread main exiting due to uncaught exception
08-22 13:13:01.020: E/AndroidRuntime(785): java.lang.NullPointerException
08-22 13:13:01.020: E/AndroidRuntime(785):  at com.example.soundplayer.MainActivity.managerOfSound(MainActivity.java:57)
08-22 13:13:01.020: E/AndroidRuntime(785):  at com.example.soundplayer.MainActivity$1.onClick(MainActivity.java:30)
08-22 13:13:01.020: E/AndroidRuntime(785):  at android.view.View.performClick(View.java:2364)
08-22 13:13:01.020: E/AndroidRuntime(785):  at android.view.View.onTouchEvent(View.java:4179)
08-22 13:13:01.020: E/AndroidRuntime(785):  at android.widget.TextView.onTouchEvent(TextView.java:6541)
08-22 13:13:01.020: E/AndroidRuntime(785):  at android.view.View.dispatchTouchEvent(View.java:3709)
08-22 13:13:01.020: E/AndroidRuntime(785):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-22 13:13:01.020: E/AndroidRuntime(785):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-22 13:13:01.020: E/AndroidRuntime(785):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-22 13:13:01.020: E/AndroidRuntime(785):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-22 13:13:01.020: E/AndroidRuntime(785):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
08-22 13:13:01.020: E/AndroidRuntime(785):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
08-22 13:13:01.020: E/AndroidRuntime(785):  at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
08-22 13:13:01.020: E/AndroidRuntime(785):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
08-22 13:13:01.020: E/AndroidRuntime(785):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
08-22 13:13:01.020: E/AndroidRuntime(785):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-22 13:13:01.020: E/AndroidRuntime(785):  at android.os.Looper.loop(Looper.java:123)
08-22 13:13:01.020: E/AndroidRuntime(785):  at android.app.ActivityThread.main(ActivityThread.java:4363)
08-22 13:13:01.020: E/AndroidRuntime(785):  at java.lang.reflect.Method.invokeNative(Native Method)
08-22 13:13:01.020: E/AndroidRuntime(785):  at java.lang.reflect.Method.invoke(Method.java:521)
08-22 13:13:01.020: E/AndroidRuntime(785):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-22 13:13:01.020: E/AndroidRuntime(785):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-22 13:13:01.020: E/AndroidRuntime(785):  at dalvik.system.NativeStart.main(Native Method)

I am getting exception in this line:

 mp.start();

Thanks in advance.

  • 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-10T04:45:34+00:00Added an answer on June 10, 2026 at 4:45 am

    You have to get the result from create method and check that result before using that mediaPlayer because create function will return media player object if successfully created and NULL if it fail to create media player.

    check the description of that function in official site

    a MediaPlayer object, or null if creation failed

    One more thing You need not to call prepare() function because On successfully created MediaPlayer , prepare() will already have been called and must not be called again.

    you can check mp is null or not before calling mp.start

    if(mp!=null)
      mp.start();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am Using MediaPlayer to Play the Mp3 file but the problem is the
While trying to play video using media player, I am getting this error: Unable
I am using media player to play list of songs.But after playing for several
I am Using this code to play a .mp3 file from res/raw. The problem
I am trying to play an mp3 using the Media player class. When I
During install, I would like to play a .wav or .mp3 (using Media Player
I am using a media player instance to play a music file.I want to
Guys i able to play audio file using Mediaplayer. But i need to play
I am using the MediaPlayer class to play an mp3 file in my application.
I am using Yahoo Media Player for playing MP3 songs on my website. I

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.