0

I need to get the lyrics from mp3 song

Mp3File mp3file = new Mp3File(file);
       
if (mp3file.hasId3v2Tag()) {

   ID3v2 id3v2Tag = mp3file.getId3v2Tag();
   Log.d("Lyrics",id3v2Tag.getLyrics());
}

The above code only I'm using. But its returning "java.lang.NullPointerException". Please help me to solve this issue.

The full crash report

E/AndroidRuntime: FATAL EXCEPTION: main Process: in.example.mp3tag, PID: 10237 java.lang.RuntimeException: Unable to start activity ComponentInfo{in.example.mp3tag/in.example.mp3tag.Main2Activity}: java.lang.NullPointerException: println needs a message at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2904) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2986) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1671) at android.os.Handler.dispatchMessage(Handler.java:108) at android.os.Looper.loop(Looper.java:206) at android.app.ActivityThread.main(ActivityThread.java:6784) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:852) Caused by: java.lang.NullPointerException: println needs a message at android.util.Log.println_native(Native Method) at android.util.Log.d(Log.java:320) at in.example.mp3tag.Main2Activity.onCreate(Main2Activity.java:200) at android.app.Activity.performCreate(Activity.java:6984) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1235) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2857) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2986)  at android.app.ActivityThread.-wrap11(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1671)  at android.os.Handler.dispatchMessage(Handler.java:108)  at android.os.Looper.loop(Looper.java:206)  at android.app.ActivityThread.main(ActivityThread.java:6784)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:852) 

1 Answers1

0

You're getting an exception because id3v2Tag.getLyrics() returned null and you're using Log.d(String tag, String msg). See documentation regarding this method here.

public static int d (String tag, String msg) ... String: The message you would like logged. This value cannot be null.

If you want to use Log.d with possible null objects, use Log.d(String tag, String msg, Throwable tr). See documentation here.

So id3v2Tag.getLyrics() returns null. The library you're using offers little documentation but I'm assuming getLyrics() returns null in the absence of lyrics, and not an empty string. This may not be an issue for the rest of your code if you test the result of getLyrics() properly, like so:

String lyrics = id3v2Tag.getLyrics();
if (lyrics != null) {
  // Lyrics found, add your code here
} else {
  // No lyrics
}
Will
  • 1,123
  • 1
  • 9
  • 22
  • Yeah tried with checking null. But the mp3 which I'm using having lyrics in it. I could able to check that in online. But here it's returning NULL, I couldn't understand the problem. Please help – Bakkiya Lakshmi Esaiarasan Aug 29 '20 at 14:55
  • If your song has lyrics and `getLyrics()` returns `null`, then the problem comes either from your song file, or from the library if it fails to fetch the lyrics. But at least you know why your app crashes. Test your app with another file, or try another library. – Will Aug 29 '20 at 18:09