0

I'm missing something really obvious, but I'm not used to working with classes.

I use the SpotifyLocalAPI to fetch data from Spotify (duuh). I've added a class to clean up some code (and to make my teacher happy). But now I get thrown a nullreferenceexeption, and I don't see why, since the same code does work in the form.cs.

In my class I have something like this:

namespace Lyricify
{
    class TrackHelper
    {
        private static SpotifyLocalAPI _spotify;

        public void retrieveTrackData()
        {
            _spotify = new SpotifyLocalAPI();
            StatusResponse status = _spotify.GetStatus();
            string test = status.Track.TrackResource.Name;
            Console.WriteLine(test);
            this.track = status.Track.TrackResource.Name;
            this.artist = status.Track.ArtistResource.Name;
            this.album = status.Track.AlbumResource.Name;
        }
    }
}

I get a NullReferenceExeption on the string test line, and on the this. lines. I know I'm missing something basic, but I have no idea where to look. I call this function like this:

TrackHelper _track = new TrackHelper();
//Different scope:
_track.retrieveTrackData();

Can somebody point me to what I'm missing here? The way I see it is that I declare the _spotify, put the .GetStatus() output into the status variable and try to write data from that variable into the string test. But appearently I'm missing something...

MagicLegend
  • 328
  • 1
  • 5
  • 22
  • From what I can tell, `GetStatus` is returning null, as such `status` is null when you try to access its Track – Alfie Goodacre Dec 13 '16 at 10:01
  • Yup. You're right. I've placed the code in the `form.cs`, and tested it there. It works fine when I remove the `_spotify = new SpotifyLocalAPI();` line, since it's already declared there. But, I can't just do that in the class, since it'd not know what `_spotify` is. How am I supposed to do that? – MagicLegend Dec 13 '16 at 10:11
  • after setting `status` you can have a null check, `if(status == null){this.track = "No song playing";}else{//your code}` Alternatively, you can also use a null coalesce operator for this, like so `this.track = status?.Track.TrackResource.Name ?? "No song playing";` This code checks if status is null: `status?`, if it isn't, it does what your code would have done anyway - hence it is followed by `.Track` etc. However if it is null, it returns "No song playing" – Alfie Goodacre Dec 13 '16 at 10:13
  • But, there ís a song playing (well, on pause, but that results in the same). The code I added in the `form.cs` does work, and correctly returns the current song. `StatusResponse status = _spotify.GetStatus(); string test = status.Track.TrackResource.Name; Console.WriteLine(test);` with the `_spotify = new SpotifyLocalAPI();` in a different scope above it. – MagicLegend Dec 13 '16 at 10:15
  • The `form.cs` returns it correctly: [image (at imgur.com)](http://i.imgur.com/N0PI9Nt.png) – MagicLegend Dec 13 '16 at 10:18

0 Answers0