in my media player i play a song from sdcard. it shows error as NullPointerException : println needs a message e in android. i tried long time but i do not know the reason .please assist me.
code:
try { mediaPlayer = new MediaPlayer(); mediaPlayer.setDataSource("/sdcard/t1.mp3"); seek.setMax(mediaPlayer.getDuration()); mediaPlayer.prepare(); mediaPlayer.start(); mediaPlayer.setOnCompletionListener(this); } catch(Exception ex){ Log.e("sdcard-err2:",ex.getMessage()); // null pointer exception : println needs a message }
Log cat:
05-16 19:27:54.491: ERROR/AndroidRuntime(6889): Caused by: java.lang.NullPointerException: println needs a message 05-16 19:27:54.491: ERROR/AndroidRuntime(6889): at android.util.Log.println(Native Method) 05-16 19:27:54.491: ERROR/AndroidRuntime(6889): at android.util.Log.e(Log.java:208) 05-16 19:27:54.491: ERROR/AndroidRuntime(6889): at com.seek.bar.media3.onCreate(media3.java:43) 05-16 19:27:54.491: ERROR/AndroidRuntime(6889): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 05-16 19:27:54.491: ERROR/AndroidRuntime(6889): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) 05-16 19:27:54.491: ERROR/AndroidRuntime(6889): ... 11 more
How to fix the NullPointerException? To avoid NullPointerException we have to initialize the Textview component with the help of findviewbyid( ) method as shown below. The findViewbyId( ) takes the “id” value of the component as the parameter. This method helps locate the component present in the app.
In Java, the java. lang. NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.
java.lang.NullPointerException. Thrown when an application attempts to use null in a case where an object is required. These include: Calling the instance method of a null object. Accessing or modifying the field of a null object.
What Causes NullPointerException. The NullPointerException occurs due to a situation in application code where an uninitialized object is attempted to be accessed or modified. Essentially, this means the object reference does not point anywhere and has a null value.
For anyone else that gets this, try replacing the lone method call or variable name with "" + varName.
For example,
Log.d("LOGCAT", getErrorMsg());
becomes
Log.d("LOGCAT", "" + getErrorMsg());
In the catch, use:
String err = (ex.getMessage()==null)?"SD Card failed":ex.getMessage(); Log.e("sdcard-err2:", err);
Passing null
to the second argument of a Log
logging function throws that error, regardless of the source.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With