50

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
kenju
  • 5,866
  • 1
  • 41
  • 41
M.A.Murali
  • 9,988
  • 36
  • 105
  • 182

6 Answers6

80

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());
Kyle Clegg
  • 38,547
  • 26
  • 130
  • 141
61

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.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Haphazard
  • 10,900
  • 6
  • 43
  • 55
11

Maybe there's simply no message attached in exception you're catching. Try ex.printStackTrace(); instead. Hope this helps.

Egor
  • 39,695
  • 10
  • 113
  • 130
  • 1
    I have been trying to debug some jni code that was actually working right and your answer allowed me to pinpoint the error to a java method instead. My jni would call a java method and this method was the one with the problem but the exception would be thrown through the jni code and it would be caught by a catch exception with ex.getMessage() which would then throw the "println needs a message". Your simple suggestion allowed me to find the bug! – Luis Mar 05 '12 at 01:13
  • @Leco, I am on similar situation as well. I hope the method just accepts empty string and just print nothing. – Neon Warge Jun 30 '17 at 08:46
7

Another way that can to use is with Log.getStackTraceString(e), example:

Log.e(TAG, Log.getStackTraceString(e));

You can read more about that in Android Documentation.

Moises Portillo
  • 828
  • 8
  • 12
3

"println needs a megssage" was a very confusing message to receive (at least for me) when not using the println(...) method at all!!!

In my case and for that matter in all cases that generate this type of error there is a constellation you have to watch out for:

try{
...
}catch(..){
  // in here there is a reference to the Logger
}

Now the problem is that in your try{...} block you are using an uninitialized reference or some old instance that now points to null

Setup a break-point at the very beginning of your try block and debug your code step by step, you will find that at some point the code will try to access methods of some object that is null.

In this case I tend to blame your seek object !

Regards.

VeRo
  • 956
  • 13
  • 27
0

My answer is useful only when you are 100% sure you have tried all the up and down answers related to Log.

For me it was not at all about logs, @Vero's answer helped me to narrow down my problem.

One of my variable inside try catch block was null at some point of time in execution and hence I was getting this exception, to resolve it I just checked null for all the variables inside try catch block and that is it.

Though you should check why that variable is getting null but that is not part of this issue I guess ;)

Resolved.

Soumyaansh
  • 8,626
  • 7
  • 45
  • 45