如何从.mp3文件列表中获取元数据并将其存储在ArrayLists中?

时间:2015-04-24 13:41:25

标签: javafx metadata changelistener

以下代码应该是:

  • 使用FileChooser
  • 导入音乐文件
  • 获取标题和艺术家的metadata值,并将其存储在两个ArraList
  • 使用test()方法打印列表中的值。

问题是当我运行open()方法时,我得到了nullpointer异常。我认为ChangeListener实际上并没有做任何事情,也无法弄清楚原因。

public class ManagerController implements Initializable {
@Override
public void initialize(URL location, ResourceBundle resources) {

}

private Stage stage;
private List<File> filelist;
private ArrayList<String> artist;
private ArrayList<String> title;

public void test(){
    for(int i = 0; i < filelist.size(); i++){
        System.out.println(title.get(i));
        System.out.println(artist.get(i));
    }
}

public void handleMetadata(String key, Object value){
    if (key.equals("title")){
        title.add(value.toString());
    }
    if (key.equals("artist")){
        artist.add(value.toString());
    }
}

public void open(){
    FileChooser chooser = new FileChooser();
    filelist = chooser.showOpenMultipleDialog(stage);
    for(File f:filelist){
    try {
        Media media = new Media(f.toURI().toURL().toString());
        media.getMetadata().addListener(new MapChangeListener<String, Object>(){
            @Override
            public void onChanged(Change<? extends String, ? extends Object> change) {
                if(change.wasAdded()) {
                    handleMetadata(change.getKey(), change.getValueAdded());
                }
            }
        });
    } catch (MalformedURLException e) {
        e.printStackTrace();
        }
    }
}

}

例外:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at com.musicstuff.musicmanager.ManagerController.handleMetadata(ManagerController.java:56)
at com.musicstuff.musicmanager.ManagerController$1.onChanged(ManagerController.java:73)
at com.sun.javafx.collections.MapListenerHelper$SingleChange.fireValueChangedEvent(MapListenerHelper.java:163)
at com.sun.javafx.collections.MapListenerHelper.fireValueChangedEvent(MapListenerHelper.java:72)
at com.sun.javafx.collections.UnmodifiableObservableMap.callObservers(UnmodifiableObservableMap.java:65)
at com.sun.javafx.collections.UnmodifiableObservableMap.lambda$new$20(UnmodifiableObservableMap.java:59)
at com.sun.javafx.collections.UnmodifiableObservableMap$$Lambda$276/1777882240.onChanged(Unknown Source)
at javafx.collections.WeakMapChangeListener.onChanged(WeakMapChangeListener.java:88)
at com.sun.javafx.collections.MapListenerHelper$SingleChange.fireValueChangedEvent(MapListenerHelper.java:163)
at com.sun.javafx.collections.MapListenerHelper.fireValueChangedEvent(MapListenerHelper.java:72)
at com.sun.javafx.collections.ObservableMapWrapper.callObservers(ObservableMapWrapper.java:115)
at com.sun.javafx.collections.ObservableMapWrapper.put(ObservableMapWrapper.java:173)
at javafx.scene.media.Media.updateMetadata(Media.java:531)
at javafx.scene.media.Media.access$200(Media.java:78)
at javafx.scene.media.Media$_MetadataListener.lambda$onMetadata$10(Media.java:542)
at javafx.scene.media.Media$_MetadataListener$$Lambda$281/790426132.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/1144405258.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/237061348.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/2117255219.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)

0 个答案:

没有答案