Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException : println needs a message in android

Tags:

android

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 
like image 648
M.A.Murali Avatar asked May 16 '11 14:05

M.A.Murali


People also ask

How do I fix NullPointerException in Android?

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.

How do I fix NullPointerException?

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.

What is NullPointerException in Android?

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 could be the reason if you are getting NullPointerException?

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.


2 Answers

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()); 
like image 186
Kyle Clegg Avatar answered Sep 21 '22 10:09

Kyle Clegg


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.

like image 36
Haphazard Avatar answered Sep 19 '22 10:09

Haphazard