Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7753845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:09:20+00:00 2026-06-01T12:09:20+00:00

I am having trouble using the Add method for an ObservableCollection to simply add

  • 0

I am having trouble using the Add method for an ObservableCollection to simply add a new string value to the observablecollection upon a click event. I create my ObservableCollection in a Settings.cs class and then reference that observablecollection throughout multiple pages in my wp7.1 project. This system has worked perfectly for when I need to add several items of one observablecollection to another, either setting one equal to the other or using .Union depending on the purpose needed. In this case though, I am attempting to add a single string item to my ObservableCollection of type string. My code is as follows

Settings.cs

public static Setting<ObservableCollection<string>> Favorites = new Setting<ObservableCollection<string>>("Favorites", null);

Favorites.xaml

<ListBox x:Name="FavoritesListBox" Grid.Row="1" ItemsSource="{Binding}" Margin="12,0,12,0"
             SelectionChanged="FavoritesListBox_SelectionChanged">

FavoritesPage.xaml.cs

protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        string favorUrl = null;
        NavigationContext.QueryString.TryGetValue("curUrl", out favorUrl);
        if (favorUrl != null )
        {
            //This works but the FavoritesListBox items are cleared upon new page navigation or closing
            //this.FavoritesListBox.Items.Add(favorUrl);

            //This does not work!?
            //if (Settings.Favorites.Value == null)
            //{
            //    //Settings.Favorites.Value.Add(favorUrl);
            //}
            //else
            //{
            //    Settings.Favorites.Value.Add(favorUrl);
            //}                     
        }

        //base.OnNavigatedTo(e);
    }

private void FavoritesListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        this.NavigationService.Navigate(new Uri("/MainPage.xaml?favUrl=" + e.AddedItems[0], UriKind.Relative));
    }

using the .Add method in FavoritesPage.xaml.cs does not give me any coding errors but when debugging I get a NullReferenceException. I also tried using .Insert and that did not work either. Please help this seems to be an easy fix but I have not been able to figure this out! Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:09:22+00:00Added an answer on June 1, 2026 at 12:09 pm

    You’re referencing a null object after confirming that it is null!

    if (Settings.Favorites.Value == null)
    {
      Settings.Favorites.Value.Add(favorUrl);  // throws NullReferenceException 
                                               // because Value is null
    }
    

    You need to do this:

    if (Settings.Favorites.Value == null)
    {
      Settings.Favorites.Value = new ObservableCollection<string>();
    }
    Settings.Favorites.Value.Add(favorUrl);
    

    Alternately, you can change the initialization from

    public static Setting<ObservableCollection<string>> Favorites = 
        new Setting<ObservableCollection<string>>("Favorites", null);
    

    to

    public static Setting<ObservableCollection<string>> Favorites = 
        new Setting<ObservableCollection<string>>("Favorites", 
          new ObservableCollection<string>());
    

    This way you can avoid the null check.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble using delayed deltas with thinking_sphinx. This is the model: Event has_many
I'm new to iPhone development and I'm having trouble using the 3rd part Kal
I'm having trouble with a Mac App Store submission. I'm using the method below
im pretty new to extjs and having trouble with it i am using the
Anybody having trouble using AVFoundation with GC ON??? What I am experiencing is that
I've been having trouble using cucumber and webrat to test authlogic-openid authentication in a
I'm having trouble using os.utime to correctly set the modification time on the mac
I am having trouble using EnumSet on the client side. I get this runtime
I'm having trouble using newly installed gems - I think there must be something
I'm having trouble using std::auto_ptr . I try to compile the following on Ubuntu

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.