System.DivideByZeroException in UWP

0

So, i did some updates to my microsoft visual studio. Previously, my UWP flipview is working perfectly. Basically my Flipview reads images from local pictures library. And now if i run my UWP, this exception appears!

System.DivideByZeroException
  HResult=0x80020012
  Message=Attempted to divide by zero.
  Source=TMD_Latest
  StackTrace:
   at TMD_Latest.Views.MainPage.ChangeImage(Object sender, Object o) in C:\Users\alish\source\repos\TMD_Latest\TMD_Latest\Views\MainPage.xaml.cs:line 97



 private void ChangeImage(object sender, object o)
        {

            //Get the number of items in the flip view
            var totalItems = TheFlipView.Items.Count;
            //Figure out the new item's index (the current index plus one, if the next item would be out of range, go back to zero)


//This line below is the exception!
            var newItemIndex = (TheFlipView.SelectedIndex + 1) % totalItems;

            //Set the displayed item's index on the flip view

            TheFlipView.SelectedIndex = newItemIndex;
        }

My xaml:

<Grid VariableSizedWrapGrid.ColumnSpan="5"
                  VariableSizedWrapGrid.RowSpan="5"
                  HorizontalAlignment="Left"
                  VerticalAlignment="Top"
                  Padding="0 30 20 20"
                   Margin="200,-100,0,10"
                  Background="Transparent">
                <x21:Grid.RowDefinitions>
                    <x21:RowDefinition Height="405*"/>
                    <x21:RowDefinition Height="21*"/>
                    <x21:RowDefinition Height="425*"/>
                </x21:Grid.RowDefinitions>
                <FlipView x:Name="TheFlipView"
            SelectionChanged="DisplayedItemChanged" Margin="-235,205.6,30,-1221.6" x21:Grid.Row="2"  >
                    <FlipView.ItemTemplate>
                        <DataTemplate>
                            <Grid Margin="0,0,0,10" >
                                <Image HorizontalAlignment="Center"  VerticalAlignment="Stretch"  Source="{Binding}"
                        Stretch="Fill" Margin="0,-200,0,0" />
                            </Grid>
                        </DataTemplate>
                    </FlipView.ItemTemplate>
                </FlipView>

            </Grid>

Please help :(

c#
image
visual-studio
uwp
flipview
asked on Stack Overflow Feb 10, 2019 by alisa fathima • edited Feb 10, 2019 by alisa fathima

1 Answer

2

As @Abestrad mentioned, this occurs because the right hand side of the remainder operator is zero. As mentioned here:

The result of x % y is the value produced by x - (x / y) * y. If y is zero, a System.DivideByZeroException is thrown.

One way to solve this in your case is to just wrap the remainder operator in an if condition:

if (totalItems > 0)
{
    var newItemIndex = (TheFlipView.SelectedIndex + 1) % totalItems; 

    //Set the displayed item's index on the flip view 
    TheFlipView.SelectedIndex = newItemIndex;
}
answered on Stack Overflow Feb 10, 2019 by devNull

User contributions licensed under CC BY-SA 3.0