Nullpointer when writing to null fields with Tag.setField()

Issue #61 invalid
David Wilmer created an issue

I'm trying to fill in empty fields (specifically the title) with the .setField method. For mp3s with empty titles (or any other field) whenever I try to .setField(), I get a Nullpointer. This only occurs when the field is empty; the nullpointer goes away when I manually type in some characters into the fields using Windows Explorer, at which point .setField() functions normally in overwriting titles. Is this intentional, with .setField() designed to work only in overwriting non-null fields? If so, which methods should I use to set null fields to some other text value?

                    artisttitle = Attribute(name,Exceptiona1,Exceptiona2,Exceptionb1,Exceptionb2);
                    Attribute2(artisttitle,i);
                    if(artisttitle.endsWith(".mp3.mp3")){
                            int dot = artisttitle.lastIndexOf('.');
                            artisttitle = artisttitle.substring(0, dot);
                    }       
                    System.out.println("After Attribute2(), artisttitle is "+artisttitle);
                    String newPath = directory + "\\"+artisttitle;
                    file.renameTo(new File(newPath));
                    System.out.println(name + " changed to " + newPath);
                    tag.addField(FieldKey.TITLE,artisttitle);
                    f.commit();

Comments (10)

  1. IJabz repo owner

    Im not sure I follow you to remove a field you should use deleteField() not set it to null. When setting a field if the field it doesnt matter what the existing value is or if there is no existing value for that field Your code sample poesnt seem to relate to the issue.

  2. yuanwofei NA

    I have encountered this problem like David Wilmer in a android project too, how to slove it? Think you

  3. IJabz repo owner

    Please post more details as the original issue code sample doesn't relate to the actual question so I can do nothing with it.

  4. yuanwofei NA

    The Code:

    private void saveModify(Music music, final ActionListener listener) {
            try {
                listener.doAction();
    
                TagOptionSingleton.getInstance().setAndroid(true);
    
                AudioFile audioFile = AudioFileIO.read(new File(music.data));
    
                Tag tag = audioFile.getTag();
                if (tag == null) {
                    Log.d("TAG", "tag is null");
                }
                tag.setField(FieldKey.TITLE, music.title);
                tag.setField(FieldKey.ARTIST, music.artist);
                tag.setField(FieldKey.ALBUM, music.album);
                audioFile.commit();
    
                refreshSystemMediaStore(music.data);
                Toasts.show(getContext(), getString(R.string.music_info_modify_success));
            } catch (Exception e) {
                Toasts.show(getContext(), getString(R.string.music_info_modify_failure));
                e.printStackTrace();
            }
    }
    

    The error:

    W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'void org.jaudiotagger.tag.Tag.setField(org.jaudiotagger.tag.FieldKey, java.lang.String)' on a null object reference W/System.err: at com.yuanwofei.music.fragment.music.ActionsFragment.saveModify(ActionsFragment.java:201) W/System.err: at com.yuanwofei.music.fragment.music.ActionsFragment.access$000(ActionsFragment.java:36) W/System.err: at com.yuanwofei.music.fragment.music.ActionsFragment$4.onClick(ActionsFragment.java:181) W/System.err: at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157) W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102) W/System.err: at android.os.Looper.loop(Looper.java:148) W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5432) W/System.err: at java.lang.reflect.Method.invoke(Native Method) W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:735) W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) D

    1.png 2.png

    the mp3

  5. IJabz repo owner

    I would think looking at that that the music object is probably null, can you check the value of music and music.artist, music.title, music.album before you call setField()

  6. yuanwofei NA

    When i test it in eclipse, i found that the follow code audioFile.getTag() will return null and throw excption.

        public static void main(String[] args) throws Exception {
    
            File file = new File("C:\\Users\\fayuan\\Desktop\\test.mp3");
    
            AudioFile audioFile = AudioFileIO.read(file);
    
            Tag tag = audioFile.getTag();
    
            tag.setField(FieldKey.TITLE, "mm");
            tag.setField(FieldKey.ARTIST, "ss");
            tag.setField(FieldKey.ALBUM, "ee");
            audioFile.commit();
        }
    

    So i change the code to

        public static void main(String[] args) throws Exception {
    
            File file = new File("C:\\Users\\fayuan\\Desktop\\test.mp3");
    
            AudioFile audioFile = AudioFileIO.read(file);
    
            Tag tag = audioFile.getTag();
            if (tag == null) {
                audioFile.setTag(new ID3v23Tag()); 
            }
            tag = audioFile.getTag();
    
            tag.setField(FieldKey.TITLE, "mm");
            tag.setField(FieldKey.ARTIST, "ss");
            tag.setField(FieldKey.ALBUM, "ee");
            audioFile.commit();
        }
    

    and it work ! Can you tell what is the problem and the best way to slove it?

  7. yuanwofei NA

    It also work in android, the code is

        private void saveModify(Music music, final ActionListener listener) {
            try {
                listener.doAction();
    
                TagOptionSingleton.getInstance().setAndroid(true);
    
                AudioFile audioFile = AudioFileIO.read(new File(music.data));
    
                Tag tag = audioFile.getTag();
                //处理jaudiotagger处理MP3的bug
                if (tag == null && music.data.toLowerCase().endsWith(".mp3")) {
                    audioFile.setTag(new ID3v23Tag());
                    if (!doModify(audioFile, audioFile.getTag(), music)) {
                        audioFile.setTag(new ID3v11Tag());
                        if (!doModify(audioFile, audioFile.getTag(), music)) {
                            Toasts.show(getContext(), getString(R.string.music_info_modify_failure));
                        }
                    }
                    return;
                }
    
                doModify(audioFile, audioFile.getTag(), music);
    
                refreshSystemMediaStore(music.data);
            } catch (Exception e) {
                Toasts.show(getContext(), getString(R.string.music_info_modify_failure));
                e.printStackTrace();
            }
        }
    
        boolean doModify(AudioFile audioFile, Tag tag, Music music) {
            try {
                tag.setField(FieldKey.TITLE, music.title);
                tag.setField(FieldKey.ARTIST, music.artist);
                tag.setField(FieldKey.ALBUM, music.album);
                audioFile.commit();
    
                Toasts.show(getContext(), getString(R.string.music_info_modify_success));
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }
    
  8. IJabz repo owner

    ok so the problem is that an mp3 may not have an ID3 tag, so what you can do is use audioFile.getTagOrCreateAndSetDefault() instead of audioFile.getTag()

  9. Log in to comment