i have a page with a collection view that lists the students at a school. when i select a student i should get the data about this student. the thing is that when i select the student i get this exception:
**System.ArgumentNullException:** 'Value cannot be null.
Parameter name: item'
the problem is that it was working well. when i made some changes to other pages and added some new pages, the error emerged. moreover, i inserted a break point at the line where i select a student to check if the values are null, but the result was that they had values. and this confused me.
this is the code of my xaml.cs page:
HttpClient client = new HttpClient();
List<registeredstdinfo> list_total_reg_std = new List<registeredstdinfo>();
List<registeredstdinfo> list_filtered_reg_std = new List<registeredstdinfo>();
bool filtered = false;
string year_id = "";
string pro = "";
public Accounting()
{
InitializeComponent();
imgAdd.Source = ImageSource.FromResource("ALNahrainAlphaApp.icons.add_col.png");
imgfilter.Source = ImageSource.FromResource("ALNahrainAlphaApp.icons.filter_ic.png");
imgshare.Source = ImageSource.FromResource("ALNahrainAlphaApp.icons.share_icon.png");
year_id = Preferences.Get("AcademicYearId","");
if (Preferences.Get("permission", "")== "Nursery & kgs")
{
pro = "GetAllregstdsForKgs";
}
else
{
pro = "GetAllregstds";
}
loaddata();
var AddTap = new TapGestureRecognizer();
AddTap.Tapped += async (sender, e) =>
{
var stack = this.Navigation.NavigationStack;
if (stack[stack.Count - 1].GetType() != typeof(RegisterNewStd))
{
await Navigation.PushAsync(new RegisterNewStd());
}
};
stckAddStd.GestureRecognizers.Add(AddTap);
ICommand refreshCommand = new Command(() =>
{
if(refresh.IsRefreshing)
{
loaddata();
}
refresh.IsRefreshing = false;
});
refresh.Command = refreshCommand;
var FilterTap = new TapGestureRecognizer();
FilterTap.Tapped += FilterTap_Tapped;
stckfilter.GestureRecognizers.Add(FilterTap);
}
async private void loaddata()
{
popupLoadingView.IsVisible = true;
activityIndicator.IsRunning = true;
urlClass urldata = new urlClass();
string uri = urldata.url + "/"+pro+"/"+year_id;
list_total_reg_std.Clear();
try
{
HttpResponseMessage response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode == true)
{
string content = await response.Content.ReadAsStringAsync();
list_total_reg_std = JsonConvert.DeserializeObject<List<registeredstdinfo>>(content);
popupLoadingView.IsVisible = false;
activityIndicator.IsRunning = false;
registeredstdslist.ItemsSource = list_total_reg_std;
foreach (var registered in list_total_reg_std)
{
if (string.IsNullOrEmpty(registered.ExpectedDateofPayment))
{
registered.ExpectedDateofPayment = "...";
}
if (registered.ExpectedDateofPayment == DateTime.Today.ToString("dd/MM/yyyy"))
registered.status = 1;
else if( DateTime.Today== DateTime.ParseExact(registered.ExpectedDateofPayment, "dd/MM/yyyy", null).AddDays(-5))
registered.status = 2;
else
registered.status = 0;
if (registered.Dateofpayment == DateTime.Today.ToString("dd/MM/yyyy"))
registered.payedtoday = true;
else
registered.payedtoday = false;
}
}
else
{
await DisplayAlert("Operation Failed", "Response Failed!", "Cancel");
}
}
catch (System.Net.WebException exp)
{
bool ans = await DisplayAlert("Connection Failed", "Please Check Your Internet Connection!", "Retry", "Cancel");
if (ans == true)
loaddata();
}
catch (Exception exp)
{
bool ans = await DisplayAlert("Connection Failed", exp.Message, "Retry", "Cancel");
if (ans == true)
loaddata();
}
}
async private void registeredstdslist_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
registeredstdinfo selecteditem= registeredstdslist.SelectedItem as registeredstdinfo;
await Navigation.PushAsync(new stdregisteredDetails(selecteditem.stdid,selecteditem.name,selecteditem.edlevel));
}
}
this is were i select a student:
async private void registeredstdslist_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
registeredstdinfo selecteditem= registeredstdslist.SelectedItem as registeredstdinfo;
await Navigation.PushAsync(new stdregisteredDetails(selecteditem.stdid,selecteditem.name,selecteditem.edlevel));
}
this is the xaml page:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ALNahrainAlphaApp.Accounting"
xmlns:images="clr-namespace:ALNahrainAlphaApp;assembly=ALNahrainAlphaApp">
<ContentPage.ToolbarItems >
<ToolbarItem Order="Secondary"
Text="logout"
Priority="2"
Clicked="ToolbarItem_Clicked"
/>
<ToolbarItem Order="Secondary"
Text="View Total Income"
Priority="2"
Clicked="ToolbarItem_Clicked_1"
/>
</ContentPage.ToolbarItems>
<ContentPage.Resources>
<Style TargetType="Grid">
<Setter Property="VisualStateManager.VisualStateGroups">
<VisualStateGroupList>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="Selected">
<VisualState.Setters>
<Setter Property="BackgroundColor"
Value="#f4f0ec" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</Setter>
</Style>
</ContentPage.Resources>
<ContentPage.Content>
<AbsoluteLayout Padding="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<StackLayout BackgroundColor="White" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All">
<StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" >
<SearchBar HorizontalOptions="FillAndExpand" x:Name="search" TextChanged="search_TextChanged"/>
<RefreshView x:Name="refresh" >
<CollectionView x:Name="registeredstdslist" SelectionChanged="registeredstdslist_SelectionChanged" SelectionMode="Single" >
<CollectionView.Header>
<Grid Padding="2" ColumnSpacing="1" RowSpacing="1">
<Grid.RowDefinitions>
<RowDefinition Height="35"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.4*"/>
<ColumnDefinition Width="0.5*"/>
<ColumnDefinition Width="0.5*"/>
<ColumnDefinition Width="0.3*"/>
<ColumnDefinition Width="0.2*"/>
<ColumnDefinition Width="0.3*"/>
</Grid.ColumnDefinitions>
<Label
Grid.Column="0"
Text="StdID"
TextColor="Black"
VerticalOptions="Center"
HorizontalOptions="Center"
LineBreakMode="TailTruncation"
/>
<Label
Grid.Column="1"
Text="Name"
TextColor="Black"
VerticalOptions="Center"
HorizontalOptions="Center"
LineBreakMode="TailTruncation"
/>
<Label
Grid.Column="2"
Text="Expected Date"
TextColor="Black"
VerticalOptions="Center"
HorizontalOptions="Center"
LineBreakMode="TailTruncation"
/>
<Label
Grid.Column="3"
Text="Edlevel"
TextColor="Black"
VerticalOptions="Center"
HorizontalOptions="Center"
LineBreakMode="TailTruncation"
/>
<Label
Grid.Column="4"
Text="Grade"
TextColor="Black"
VerticalOptions="Center"
HorizontalOptions="Center"
LineBreakMode="TailTruncation"
/>
<Label
Grid.Column="5"
Text="Status"
TextColor="Black"
VerticalOptions="Center"
HorizontalOptions="Center"
LineBreakMode="TailTruncation"
/>
</Grid>
</CollectionView.Header>
<CollectionView.ItemTemplate>
<DataTemplate >
<Grid ColumnSpacing="1" RowSpacing="0" Padding="9,0,0,0" >
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.4*"/>
<ColumnDefinition Width="0.5*"/>
<ColumnDefinition Width="0.5*"/>
<ColumnDefinition Width="0.3*"/>
<ColumnDefinition Width="0.2*"/>
<ColumnDefinition Width="0.3*"/>
</Grid.ColumnDefinitions>
<Grid.Triggers>
<DataTrigger TargetType="Grid" Binding="{Binding payedtoday}" Value="false" >
<Setter Property="BackgroundColor" Value="Transparent"/>
</DataTrigger>
<DataTrigger TargetType="Grid" Binding="{Binding payedtoday}" Value="true" >
<Setter Property="BackgroundColor" Value="LightYellow"/>
</DataTrigger>
</Grid.Triggers>
<Label
Grid.Column="0"
Text="{Binding stdid}"
TextColor="Black"
VerticalOptions="Center"
HorizontalOptions="Center"
LineBreakMode="TailTruncation"
FontSize="13"
/>
<Label
Grid.Column="1"
Text="{Binding name}"
TextColor="Black"
VerticalOptions="Center"
HorizontalOptions="Center"
LineBreakMode="TailTruncation"
FontSize="13"
/>
<Label
Grid.Column="2"
Text="{Binding ExpectedDateofPayment}"
TextColor="Black"
VerticalOptions="Center"
HorizontalOptions="Center"
LineBreakMode="TailTruncation"
FontSize="13"
/>
<Label
Grid.Column="3"
Text="{Binding edlevel}"
TextColor="Black"
VerticalOptions="Center"
HorizontalOptions="Center"
LineBreakMode="TailTruncation"
FontSize="13"
/>
<Label
Grid.Column="4"
Text="{Binding grade}"
TextColor="Black"
VerticalOptions="Center"
HorizontalOptions="Center"
LineBreakMode="TailTruncation"
Padding="10"
FontSize="13"
/>
<Image
Grid.Column="5"
VerticalOptions="Center"
HorizontalOptions="Center"
HeightRequest="20"
WidthRequest="20"
>
<Image.Triggers>
<DataTrigger TargetType="Image" Binding="{Binding status}" Value="0" >
<Setter Property="Source" Value="{OnPlatform Android=redtik.png,iOS=redtik.png}"/>
</DataTrigger>
<DataTrigger TargetType="Image" Binding="{Binding status}" Value="1" >
<Setter Property="Source" Value="{OnPlatform Android=greentik.png, iOS=greentik.png}"/>
</DataTrigger>
<DataTrigger TargetType="Image" Binding="{Binding status}" Value="2" >
<Setter Property="Source" Value="{OnPlatform Android=browntick.png, iOS=browntick.png}"/>
</DataTrigger>
</Image.Triggers>
</Image>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</RefreshView>
<StackLayout VerticalOptions="EndAndExpand" HorizontalOptions="FillAndExpand" Orientation="Horizontal" BackgroundColor="#0d98ba">
<StackLayout Style="{StaticResource ButtonNavigationBarStackLayoutStyle}" x:Name="stckAddStd">
<Image Margin="0,10,0,5" x:Name="imgAdd" Style="{StaticResource ButtonNavigationBarImageStyle}" />
</StackLayout>
<StackLayout Style="{StaticResource ButtonNavigationBarStackLayoutStyle}" x:Name="stckfilter">
<Image Margin="0,10,0,10" x:Name="imgfilter" Style="{StaticResource ButtonNavigationBarImageStyle}" />
</StackLayout>
<StackLayout Style="{StaticResource ButtonNavigationBarStackLayoutStyle}" x:Name="stckshare">
<Image Margin="0,10,0,10" x:Name="imgshare" Style="{StaticResource ButtonNavigationBarImageStyle}" />
</StackLayout>
</StackLayout>
</StackLayout>
</StackLayout>
<ContentView x:Name="popupLoadingView" BackgroundColor="Transparent" Padding="10, 0" IsVisible="false" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All">
<StackLayout VerticalOptions="Center" HorizontalOptions="Center">
<StackLayout Orientation="Vertical" HeightRequest="150" WidthRequest="200" BackgroundColor="Transparent">
<ActivityIndicator x:Name="activityIndicator" Margin="0,50,0,0" VerticalOptions="Center" HorizontalOptions="Center" Color="Black" WidthRequest="40" HeightRequest="40" ></ActivityIndicator>
</StackLayout>
</StackLayout>
</ContentView>
</AbsoluteLayout>
</ContentPage.Content>
</ContentPage>
i can't figure out the reason. i removed the bin and obj folders, cleaned the solution then built it but still nothing, what should i do? thanks in advance.