0

The user enters the dimension of the matrix and the program should calculate the sum of rows, columns and the total sum of the matrix. If the dimension of the matrix is ​​the same, for example, 2x2 3x3 7x7 then everything is fine. But if a 3x2 5x4 7x5 matrix, an exception occurs: An unhandled exception of type 'System.NullReferenceException' occurred in createElement_pr_4.exe

Additional information: The object reference does not indicate an object instance.

<Window x:Class="createElement_pr_4.MainWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:local="clr-namespace:createElement_pr_4" 
mc:Ignorable="d" 
Title="" Height="600" Width="1000">
    <Grid x:Name="Container">
        <TextBox HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="24" x:Name="HeightEl"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="40,0,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="28" x:Name="WidthEl"/>
        <Button Content="Создать таблицу" HorizontalAlignment="Left" Margin="92,0,0,0" VerticalAlignment="Top" Width="120" x:Name="Create" Click="Create_Click"/>
        <Button Content="Вычислить"  HorizontalAlignment="Left" Margin="61,178,0,0" VerticalAlignment="Top" Width="75" x:Name="Add" Click="Add_Click"/>
        <Label x:Name="test" Content="" HorizontalAlignment="Left" Margin="10,533,0,-7" VerticalAlignment="Top" Height="43" Width="120"/>
        <Label x:Name="row" Content="" HorizontalAlignment="Left" Margin="158,533,0,0" VerticalAlignment="Top" Width="377"/>
        <Label x:Name="col" Content="" HorizontalAlignment="Left" Margin="580,533,0,0" VerticalAlignment="Top" Width="377"/>
        <Label x:Name="label" Content="Общая сумма" HorizontalAlignment="Left" Margin="0,482,0,0" VerticalAlignment="Top"/>
        <Label x:Name="label1" Content="Сумма строк" HorizontalAlignment="Left" Margin="158,482,0,0" VerticalAlignment="Top"/>
        <Label x:Name="label2" Content="Сумма колонок" HorizontalAlignment="Left" Margin="580,482,0,0" VerticalAlignment="Top"/>





    </Grid>
</Window>
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

namespace createElement_pr_4
{
    public partial class MainWindow : Window
    {
        int w, h;
        TextBox[,] textBoxes = new TextBox[10, 10];
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Create_Click(object sender, RoutedEventArgs e)
        {
            foreach (var TextBoxItem in textBoxes)
            {
                Container.Children.Remove(TextBoxItem);
            }

            try
            {
                h = int.Parse(HeightEl.Text);
                if (h > 10)
                {
                    h = 0;
                    MessageBox.Show("error");
                }
                w = int.Parse(WidthEl.Text);
                if (w > 10)
                {
                    w = 0;
                    MessageBox.Show("error");
                }
            }
            catch (System.FormatException)
            {
                MessageBox.Show("error");
            }


            for (int i = 0; i < h; i++)
            {
                for (int j = 0; j < w; j++)
                {
                    TextBox textBox = new TextBox()
                    {
                        Height = 23,
                        Width = 23,
                        HorizontalAlignment = HorizontalAlignment.Left,
                        VerticalAlignment = VerticalAlignment.Top,
                        Margin = new Thickness(250.0 + (double)(j * 40), 100 + (double)(i * 40), 0, 0)
                    };
                    try
                    {
                        textBoxes[i, j] = textBox;
                        Container.Children.Add(textBoxes[i, j]);
                    }
                    catch (System.IndexOutOfRangeException)
                    {
                        MessageBox.Show("error");
                    }
                }
            }
            //Add.Visibility = Visibility.Visible;
            //Result.Visibility = Visibility.Visible;
        }

        private void Add_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                double sumCol;
                double sumRow;
                double sumAll = 0;

                for (int j = 0; j < w; j++)
                {
                    sumCol = 0;
                    sumRow = 0;
                    for (int i = 0; i < h; i++)
                    {
                        sumAll += Convert.ToDouble((textBoxes[i, j]).Text);
                        sumCol += Convert.ToDouble((textBoxes[i, j]).Text);
                        sumRow += Convert.ToDouble((textBoxes[j, i]).Text);
                    }
                    col.Content += Convert.ToString(sumCol + ", ");
                    row.Content += Convert.ToString(sumRow + ", ");
                }
                test.Content = sumAll;
            }
            catch (System.FormatException)
            {
                MessageBox.Show("erroe");
            }
            catch (System.OverflowException)
            {
                MessageBox.Show("error");
            }

        }
    }
}
  • 1
    Welcome to Stack Overflow. You need to use the debugger to find out on exactly what line the exception is being thrown. Then you can figure out what's null, and how to fix that. Are you familiar with the debugger? – 15ee8f99-57ff-4f92-890c-b56153 May 28 '19 at 18:12
  • 2
    for `sumRow` you are accessing `textBoxes[j, i]` instead of `textBoxes[i, j]` – Olivier Jacot-Descombes May 28 '19 at 18:16
  • 2
    What you're doing with exceptions is not good practice: You're catching certain exceptions, but then you're hiding what went wrong. That makes it much more difficult to fix things when something does go wrong. Olivier is correct, by the way: You'll get an exception in Add_Click on any matrix that isn't square, because either i or j will be out of range at some point. You need to rethink how to calculate row sums. – 15ee8f99-57ff-4f92-890c-b56153 May 28 '19 at 18:18
  • 2
    You should catch only exceptions you are not in control of, e.g., coming from I/O, databases etc. Programming exceptions like `IndexOutOfRangeException` should be fixed, not caught! – Olivier Jacot-Descombes May 28 '19 at 18:29

0 Answers0