Dhoni
Dhoni

Reputation: 1234

Exception thrown: 'System.ArgumentNullException' in mscorlib.ni.dll

I am new to c# and trying to implement INotifyPropertyChanged for my ObsorvableCollection Class

But Its giving an error and Data doesn't got binded. Someone please help me in resolving this

An exception of type System.ArgumentNullException occurred in mscorlib.ni.dll but was not handled in user code

Additional information: Value cannot be null.

Thanks in Advance.

My Xaml code:

<Page 
x:Class="App2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App2"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Page.BottomAppBar>
    <CommandBar Background="Orange">
        <AppBarButton Icon="Sort" Label="Sort">
            <AppBarButton.Flyout>
                <MenuFlyout>
                    <MenuFlyoutItem Text="By Upvotes" Click="FilterItem_Click" Tag="name"/>
                    <MenuFlyoutItem Text="By OpenForms" Click="FilterItem_Click" Tag="lname"/>
                    <MenuFlyoutItem Text="By Ideas" Click="FilterItem_Click" Tag="ideas"/>
                </MenuFlyout>
            </AppBarButton.Flyout>
        </AppBarButton>
    </CommandBar>
</Page.BottomAppBar>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <ListView ItemsSource="{x:Bind Path=person}" Margin="105,130,95,70">
        <ListView.ItemTemplate>
            <DataTemplate x:DataType="local:Person">
                <TextBlock Text="{x:Bind Name}"></TextBlock>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
   </Grid>
</Page>

My cs Code:

        using App2.WrittenLibraries;
        using System.Collections.Generic;
        using System.Collections.ObjectModel;
        using System.ComponentModel;
        using System.Linq;
        using System.Threading.Tasks;
        using Windows.UI.Xaml;
        using Windows.UI.Xaml.Controls;

        // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

        namespace App2
        {
            /// <summary>
            /// An empty page that can be used on its own or navigated to within a Frame.
            /// </summary>
            /// 
            public class Person : INotifyPropertyChanged
            {
                private string name;
                public string Name
                {
                    get { return name;  }
                    set
                    {
                        name = value;
                        OnPropertyChanged("Name");
                    }
                }

                private string lastname;
                private string v1;
                private string v2;

                public string Lastname
                {
                    get { return lastname; }
                    set
                    {
                        lastname = value;
                        OnPropertyChanged("Lastname");
                    }
                }

                public event PropertyChangedEventHandler PropertyChanged;

                private void OnPropertyChanged(string propertyName)
                {
                    if (propertyName != null)
                    {

                        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                    }
                }


                public Person(string v1, string v2)
                {
                    this.v1 = v1;
                    this.v2 = v2;
                }
            }
            public sealed partial class MainPage : Page
             {
                public ObservableCollection<Person> person = new ObservableCollection<Person>();

                public MainPage()
                {
                    this.InitializeComponent();

                    person.Add(new Person("F1", "L1"));
                    person.Add(new Person("F2", "L2"));
                }


                private void FilterItem_Click(object sender, RoutedEventArgs e)
                {
                    MenuFlyoutItem selectedItem = sender as MenuFlyoutItem;

                    if (selectedItem != null)
                    {
                        if (selectedItem.Tag.ToString() == "name")
                        {
                            Util.debugLog("FILTER BY NAME");
                            person = new ObservableCollection<Person>(person.OrderBy(i => i.Name));

                            //FilterByUpvotes()();
                        }
                        else if (selectedItem.Tag.ToString() == "lname")
                        {
                            Util.debugLog("FILTER BY L_NAME");
                            person = new ObservableCollection<Person>(person.OrderBy(i => i.Lastname));
                            //FilterByOpenForm();
                        }
                        else if (selectedItem.Tag.ToString() == "ideas")
                        {
                            Util.debugLog("FILTER BY IDEAS");
                            //person = new ObservableCollection<Person>(person.OrderBy(i => i));

                            //FilterByIdeas();
                        }
                    }
                }
            }
        }

Upvotes: 0

Views: 11565

Answers (3)

Saurabh Junankar
Saurabh Junankar

Reputation: 1

You have to figure out from where you are getting the null value and place the code in the try catch block and write the catch block for "System.ArgumentNullException".

try
{
   `enter code here` // your code enter code here
}

catch (ArgumentNullException ex)
{
   `enter code here` //code specifically for a ArgumentNullException
}

Upvotes: 0

mohamed hassn
mohamed hassn

Reputation: 1

The first thing of solving that problem is to find which part of your code generates this null value, then try to understand the following code in c# which was very useful for me to avoid the null value by checking the retrieve code if it will read me null or not .. just like :

if(cmd.ExecuteScalar() == null)
    MessageBox.Show("data is null");
else
    //put ur code here 

Also, do not forget to use try catch block.

Upvotes: 0

Joseph
Joseph

Reputation: 1064

Your issue is in using x:bind to bind the name property. your name will always be null since you are not setting any value.so the binding fails. what you can do is edit you list view template like

       <ListView ItemsSource="{x:Bind Path=person}" Margin="105,130,95,70" Loaded="ListView_Loaded">
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="local:Person">
                    <TextBlock Text="{Binding Name}" Foreground="Red"></TextBlock>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

and inside the code while you are creating a person set the name

 public Person(string v1, string v2)
    {
        this.v1 = v1;
        this.v2 = v2;
        this.name = v1;//set name as v1
    }

Upvotes: 0

Related Questions