# NullPointerException



## Steiner2023 (17. Jun 2017)

Hi,

wir sollen einen Musikplayer programmieren, ich habe jedoch eine Exception bei der ich absolut null Plan haben, wie ich sie beheben könnte:


```
Player initialized
java.lang.NullPointerException
    at de.hofuniversity.soundmaster.data.Playlist.fromDirectory(Playlist.java:168)
    at de.hofuniversity.soundmaster.Main.start(Main.java:35)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    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$148(WinApplication.java:191)
    at java.lang.Thread.run(Unknown Source)
```

fromDirectory:


```
public static Playlist fromDirectory(File dir)
    {
        Playlist p = new Playlist();
        File[] files = dir.listFiles(new FilenameFilter()
        {   
            @Override       
            public boolean accept(File dir, String name)
            {
                return name.toLowerCase().endsWith(".mp3");
            }
        });
       
       
        for(File f : files)
        {
           
            try
            {
                AudioFile mp3 = AudioFileIO.read(f);
                Title t = new Title(mp3.getTag().getFirstTitle(), new Artist(mp3.getTag().getFirstArtist()), mp3.getLength()*1000);
                t.setFile(f);
                p.add(t);
               
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
           
        }
        return p;
    }
```

Artist:


```
package de.hofuniversity.soundmaster.data;

public class Artist
{
    private String surname, givenname;
    private long birthdate;
   
   

    public Artist(String firstArtist)
    {
    }
    public String getSurname()
    {
        return surname;
    }

    public void setSurname(String surname)
    {
        this.surname = surname;
    }

    public String getGivenname()
    {
        return givenname;
    }

    public void setGivenname(String givenname)
    {
        this.givenname = givenname;
    }

    public long getBirthdate()
    {
        return birthdate;
    }

    public void setBirthDate(long birthdate)
    {
        this.birthdate = birthdate;
    }
   
    @Override
    public String toString()
    {
        return "Artist [surname=" + surname + ", givenname=" + givenname + "]";
    }
}
```


----------



## Robat (17. Jun 2017)

Was steht denn in Zeile 168 in der Klasse `Playlist`?
NPE heißt ja bekanntlich immer dass du versuchst mit einem Objekt zu arbeiten welches noch nicht initialisiert - also `null` - ist.
Du könntest bspw. einmal prüfen was in `mp3` eig. drinne steht nachdem du `AudioFileIO.read(f);` aufgerufen hast.


----------

