0

The following code is supposed to:

  • import music files with a FileChooser
  • get the metadata values for title and artist and store them in two ArraList
  • print out the values in the lists using the test() method.

The problem is I get nullpointer exceptions when I run the open() method. I assume the ChangeListener isn't actually doing anything and can't figure out why.

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:

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)
Peralta
  • 180
  • 1
  • 12
  • Can you add the exception so we know what throws it? – Aerus Apr 24 '15 at 15:25
  • Done, I get the exception twice for each file added. – Peralta Apr 24 '15 at 17:55
  • So `handleMetadata` is called but either the key or value is `null`, which causes the NPE (I assume you instantiate `title` and `artist` somewhere else?). It would be easiest if you look with a debugger (or even with print statements) what the input is that causes this and work from there.. – Aerus Apr 24 '15 at 18:12
  • @James_D line 56 is `artist.add(value.toString());` – Peralta Apr 24 '15 at 19:13
  • And you initialized artist somewhere, right? – James_D Apr 24 '15 at 19:25
  • Do you get an exception on this line `title.add(value.toString());`? – J Atkin Apr 24 '15 at 19:39
  • @James_D Oh.. No I didn't.. That solved it. Sorry for wasting your time, I only started programming a month ago and I still forget some basic stuff – Peralta Apr 24 '15 at 20:19

0 Answers0