Handling NullPointer Exception in a MediaFile app

All your problems with Audio, Video and Images.

Handling NullPointer Exception in a MediaFile app

Postby Maxood » Sat Dec 19, 2009 10:04 am

I'm developing a media file app that is meant to play sounds stored in
my raw folder. There are about 32 sounds in all. And this is what i'm
doing on the click of each button:

Button btnGrowUp = (Button) this.findViewById(R.id.GrowUp);
btnGrowUp.setOnClickListener(btnGrowUpListener);


private OnClickListener btnGrowUpListener = new OnClickListener()
{
public void onClick(View v)
{
//Toast.makeText(getBaseContext(), "Grow Up audio file is being
played", Toast.LENGTH_SHORT).show();

MediaPlayer mp = MediaPlayer.create(MainScreen.this,
R.raw.growup);

mp.start();
}

};



I get a runtime exception saying "app stops unexpectedly" with the
follow messages in my logcat window:


12-19 12:33:05.420: WARN/dalvikvm(699): threadid=3: thread exiting
with uncaught exception (group=0x4000fe70)
12-19 12:33:05.441: ERROR/AndroidRuntime(699): Uncaught handler:
thread main exiting due to uncaught exception
12-19 12:33:05.460: ERROR/AndroidRuntime(699):
java.lang.NullPointerException
12-19 12:33:05.460: ERROR/AndroidRuntime(699): at
com.myapps.media.MainScreen$8.onClick(MainScreen.java:244)
12-19 12:33:05.460: ERROR/AndroidRuntime(699): at
android.view.View.performClick(View.java:2179)
12-19 12:33:05.460: ERROR/AndroidRuntime(699): at
android.view.View.onTouchEvent(View.java:3828)
12-19 12:33:05.460: ERROR/AndroidRuntime(699): at
android.widget.TextView.onTouchEvent(TextView.java:6291)
12-19 12:33:05.460: ERROR/AndroidRuntime(699): at
android.view.View.dispatchTouchEvent(View.java:3368)
12-19 12:33:05.460: ERROR/AndroidRuntime(699): at
android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
12-19 12:33:05.460: ERROR/AndroidRuntime(699): at
android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
12-19 12:33:05.460: ERROR/AndroidRuntime(699): at
android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
12-19 12:33:05.460: ERROR/AndroidRuntime(699): at
android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
12-19 12:33:05.460: ERROR/AndroidRuntime(699): at
android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
12-19 12:33:05.460: ERROR/AndroidRuntime(699): at
com.android.internal.policy.impl.PhoneWindow
$DecorView.superDispatchTouchEvent(PhoneWindow.java:1707)
12-19 12:33:05.460: ERROR/AndroidRuntime(699): at
com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent
(PhoneWindow.java:1197)
12-19 12:33:05.460: ERROR/AndroidRuntime(699): at
android.app.Activity.dispatchTouchEvent(Activity.java:1993)
12-19 12:33:05.460: ERROR/AndroidRuntime(699): at
com.android.internal.policy.impl.PhoneWindow
$DecorView.dispatchTouchEvent(PhoneWindow.java:1691)
12-19 12:33:05.460: ERROR/AndroidRuntime(699): at
android.view.ViewRoot.handleMessage(ViewRoot.java:1525)
12-19 12:33:05.460: ERROR/AndroidRuntime(699): at
android.os.Handler.dispatchMessage(Handler.java:99)
12-19 12:33:05.460: ERROR/AndroidRuntime(699): at
android.os.Looper.loop(Looper.java:123)
12-19 12:33:05.460: ERROR/AndroidRuntime(699): at
android.app.ActivityThread.main(ActivityThread.java:3948)
12-19 12:33:05.460: ERROR/AndroidRuntime(699): at
java.lang.reflect.Method.invokeNative(Native Method)
12-19 12:33:05.460: ERROR/AndroidRuntime(699): at
java.lang.reflect.Method.invoke(Method.java:521)
12-19 12:33:05.460: ERROR/AndroidRuntime(699): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:782)
12-19 12:33:05.460: ERROR/AndroidRuntime(699): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
12-19 12:33:05.460: ERROR/AndroidRuntime(699): at
dalvik.system.NativeStart.main(Native Method)
12-19 12:33:05.520: INFO/Process(563): Sending signal. PID: 699 SIG: 3
12-19 12:33:05.520: INFO/dalvikvm(699): threadid=7: reacting to signal
3
12-19 12:33:05.601: INFO/dalvikvm(699): Wrote stack trace to '/data/
anr/traces.txt'
12-19 12:33:05.982: INFO/ARMAssembler(563): generated
scanline__00000077:03515104_00000000_00000000 [ 27 ipp] (41 ins) at
[0x27c718:0x27c7bc] in 982527 ns
12-19 12:33:06.011: INFO/ARMAssembler(563): generated
scanline__00000077:03515104_00001001_00000000 [ 64 ipp] (84 ins) at
[0x27c7c0:0x27c910] in 1804978 ns

How to resolve this issue? Thank you

Maqsood
Maxood

"Life is an endless journey to achieve perfection"
Maxood
Developer
Developer
 
Posts: 34
Joined: Sun Aug 02, 2009 1:37 pm
Location: Karachi, Pakistan

Top

Postby qlimax » Sat Dec 19, 2009 5:08 pm

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.         try {
  4.  
  5.                         mp.start();
  6.  
  7.                 } catch (NullPointerException e) {
  8.  
  9.                         Log.v("MP error",e.toString());
  10.  
  11.                 }
  12.  
  13.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


?

btw, the create method return null if the creation failed...
when you are done with a sound, call release() on the mediaplayer. you can use setOnCompletionListener.

bye ;)
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby Maxood » Sun Dec 20, 2009 1:22 pm

Could you give me an example for code please. The null pointer exception is thrown after every eighth button is clicked(or trying to play any eighth audio file in the raw folder).
Maxood

"Life is an endless journey to achieve perfection"
Maxood
Developer
Developer
 
Posts: 34
Joined: Sun Aug 02, 2009 1:37 pm
Location: Karachi, Pakistan

Postby qlimax » Sun Dec 20, 2009 1:36 pm

qlimax wrote:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         try {
  3.                         mp.start();
  4.                 } catch (NullPointerException e) {
  5.                         Log.v("MP error",e.toString());
  6.                 }
  7.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


?

btw, the create method return null if the creation failed...
when you are done with a sound, call release() on the mediaplayer. you can use setOnCompletionListener.

bye ;)


wasn't that an example of code?
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby Maxood » Tue Dec 22, 2009 1:17 pm

Thanks a lot for your help. The app is running smoothly now. I called the release() method on the OnSetCompletionListener event.
Maxood

"Life is an endless journey to achieve perfection"
Maxood
Developer
Developer
 
Posts: 34
Joined: Sun Aug 02, 2009 1:37 pm
Location: Karachi, Pakistan

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 17 guests