×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    8

    Client throws error "Null Reference"

    Hi
    I am writing an application using NokiaMusic - btw, a very cool service, - and I encountered a small problem. I am not sure if the problem is with my programming skills, or I am using something wrong from the APIs.
    So, first things first: here is my test:
    Code:
    [TestMethod]
            public void PopulateCollectionWithGenresTest()
            {
                GenreHelper genreHelper = new GenreHelper(radioMixHelper);
                genreHelper.GetGenres();
                Assert.AreNotEqual(0, genreHelper.Genres.Count);
            }
    Here is the code for GenreHelper:
    Code:
    public class GenreHelper : ObservableObject
        {
            public RadioMixHelper MusicHelper { get; set; }
    
            private ObservableCollection<Genre> _Genres = null;
            public ObservableCollection<Genre> Genres
            {
                get { return _Genres; }
                set
                {
                    if (value != _Genres)
                    {
                        _Genres = value;
                        OnPropertyChanged("Genres");
                    }
                }
            }
    
            public List<Genre> SelectedGenres { get; set; }
    
            public GenreHelper(RadioMixHelper radioMixHelper)
            {
                this.MusicHelper = radioMixHelper;
            }
    
            public async void GetGenres()
            {
                this.Genres = await MusicHelper.GetGenres();
            }
    
        }
    And here is the code for RadioMixHelper:
    Code:
     public class RadioMixHelper
        {
            public MusicClient Client { get; set; }
    
            private string nokiaClientId = "c46a5faaf0218a8ce6a8d8d71cf08e4e";
    
            public RadioMixHelper()
            {
                this.Client = new MusicClient(nokiaClientId);
            }
            public async Task<ObservableCollection<Genre>> GetGenres()
            {
                ObservableCollection<Genre> genresCollection = null;
                try
                {
                    var genres = await this.Client.GetGenresAsync();
                    if (genres.Result == null && genres.Count == 0)
                    {
                        genresCollection = new ObservableCollection<Genre>(genres);
                    }               
                    return genresCollection;
                }
                catch (NokiaMusicException ex)
                {
                    Debug.WriteLine(ex.Message);
                    return genresCollection;
                }
            }
        }
    I tracked the error to this line in RadioMixHelper class:
    Code:
    var genres = await this.Client.GetGenresAsync();
    This line throws me the error -null reference, but I have no idea why.
    Maybe someone will notice what I am doing wrong.
    Thank You for help in advance!
    Last edited by Diver-dx; 2014-02-17 at 19:41.

  2. #2
    Nokia Developer Expert
    Join Date
    Feb 2014
    Posts
    27

    Re: Client throws error "Null Reference"

    Hi, tracing through the code you've supplied it comes down to the fact that the MusicHelper variable in GenreHelper in could be null.
    This variable is set by the constructor of GenreHelper, which in the test code is set as:
    GenreHelper genreHelper = new GenreHelper(radioMixHelper);
    ...but without knowing what you have set radioMixHelper to in the test class, I can't see what else is wrong. Put a breakpoint on that line in the test to make sure that "radioMixHelper" is initialised.

    If I may, I'd say simplify your code as well - you don't need to wrap the responses from the API with an ObservableCollection, as the results are not going to change over time, e.g. the list of genres is just that, a static list. The exception here could be search results where user input would affect the list, but there are other ways of knowing the list has changed without it having to be observable.
    My point is you can bind the list of genres straight in to a ListBox (or similar) control without the wrapper code.

    It's of course your choice and hopefully the above will help you track down the problem, but I just wanted you to know that the SDK was designed to return List<> rather than ObservableCollection<> purposefully.

    Regards,
    Steve

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    8

    Re: Client throws error "Null Reference"

    Thank You very much for the response, and especially for the hint with the ObservableCollection. However, all the variables are not null, and the error occurs when I call GetGenresAsync() from MusicClient, which also isn't null. That's the most confusing part here - it seems like there are no null objcets, and still I get the null reference exception from the API object.

  4. #4
    Nokia Developer Expert
    Join Date
    Feb 2014
    Posts
    27

    Re: Client throws error "Null Reference"

    If you want to attach a test app that reproduces the problem, I can take a look
    Regards,
    Steve

  5. #5
    Registered User
    Join Date
    Feb 2014
    Posts
    8

    Re: Client throws error "Null Reference"

    Here is the link to the app on dropbox. If You could tell me what I am doing wrong, I would be greatly grateful to You. Link: https://www.dropbox.com/sh/i0cmdwv974rx4xs/u1ClOfnpt3

  6. #6
    Nokia Developer Expert
    Join Date
    Feb 2014
    Posts
    27

    Re: Client throws error "Null Reference"

    Quote Originally Posted by Diver-dx View Post
    Here is the link to the app on dropbox. If You could tell me what I am doing wrong, I would be greatly grateful to You. Link: https://www.dropbox.com/sh/i0cmdwv974rx4xs/u1ClOfnpt3
    Running the WP app, I found that this line had the problem:
    this.genreHelper.GetGenres();
    I think you wanted
    this.GenreHelper.GetGenres();
    (Full class below for other folk's reference)

    public class MainViewModel : ObservableObject
    {
    public RadioMixHelper musicHelper = new RadioMixHelper();

    private GenreHelper genreHelper = null;
    public GenreHelper GenreHelper
    {
    get
    {
    if (genreHelper == null)
    {
    genreHelper = new GenreHelper(musicHelper);
    }
    return genreHelper;
    }
    }

    public MainViewModel()
    {
    this.genreHelper.GetGenres();
    }
    }

  7. #7
    Registered User
    Join Date
    Feb 2014
    Posts
    8

    Re: Client throws error "Null Reference"

    Thank You very much for Your help - I did't even think about misspelling something - You really helped me a lot!

    Best regards,
    Roman

Similar Threads

  1. Replies: 2
    Last Post: 2011-01-25, 06:50
  2. In S60 Emulator my application message shows "null null"
    By ramtrg in forum Mobile Java General
    Replies: 3
    Last Post: 2011-01-22, 05:27
  3. "6131 NFC" as WebService client: No class def found error: java.rmi.Remote
    By stewert in forum Mobile Java Networking & Messaging & Security
    Replies: 10
    Last Post: 2009-04-17, 13:51
  4. Replies: 1
    Last Post: 2008-06-03, 13:38
  5. Replies: 10
    Last Post: 2007-03-21, 09:02

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×