2

How can I copy the values from dynamic textboxes to a jagged array? I tried with a for cycle but I constantly get this error message:"Object reference not set to an instance of an object." What can be the problem?(the textboxes are also made with jagged arrays) Here is the full code, you can find the problematic lines in the first lines of the button1 event handler link

for (int a = 0; a < nr; a++)
         {
             for (int b = 0; b < nr+ 1; b++)
             {
                 array[a][b] =int.Parse(TB[a][b].Text);
             }
         }

(Here's the full code:)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }



           int ismeretlen = 2;
            TextBox[][] TB;
            string file = "3ismeretlen.dat";



        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int[][] egyenletek = new int[ismeretlen][];

             for (int a = 0; a < ismeretlen; a++)
             {
                 for (int b = 0; b < ismeretlen + 1; b++)
                 {
                     egyenletek[a][b] =int.Parse(TB[a][b].Text);

                 }
             }

            int változószám = TB[0].Length;
            for (int i = 0; i < változószám - 1; i++)
            {
                for (int j = i; j < változószám - 1; j++)
                {
                    int[] d = new int[változószám];
                    for (int x = 0; x < változószám; x++)
                    {
                        if (i == j && egyenletek[j][i] == 0)
                        {
                            bool changed = false;
                            for (int z = egyenletek.Length - 1; z > i; z--)
                            {
                                if (egyenletek[z][i] != 0)
                                {
                                    int[] temp = new int[változószám];
                                    temp = egyenletek[z];
                                    egyenletek[z] = egyenletek[j];
                                    egyenletek[j] = temp;
                                    changed = true;
                                }
                            }
                            if (!changed)
                            {
                                textBox1.Text += "Az egyenletrendszernek nincs megoldása!\r\n";
                                return;
                            }
                        }

                        if (egyenletek[j][i] != 0)
                        {
                            d[x] = egyenletek[j][x] / egyenletek[j][i];
                        }
                        else
                        {
                            d[x] = egyenletek[j][x];
                        }
                    }
                    egyenletek[j] = d;
                }
                for (int y = i + 1; y < egyenletek.Length; y++)
                {
                    int[] f = new int[változószám];
                    for (int g = 0; g < változószám; g++)
                    {
                        if (egyenletek[y][i] != 0)
                        {
                            f[g] = egyenletek[y][g] - egyenletek[i][g];
                        }
                        else
                        {
                            f[g] = egyenletek[y][g];
                        }
                    }
                    egyenletek[y] = f;
                }
            }
            double val = 0;
            int k = változószám - 2;
            double[] eredmény = new double[egyenletek.Length];
            for (int i = egyenletek.Length - 1; i >= 0; i--)
            {
                val = egyenletek[i][változószám - 1];
                for (int x = változószám - 2; x > k; x--)
                {
                    val -= egyenletek[i][x] * eredmény[x];
                }
                eredmény[i] = val / egyenletek[i][i];
                if (eredmény[i].ToString() == "NaN" || eredmény[i].ToString().Contains("Végtelen sok megoldás."))
                {
                    textBox1.Text += "Az egyenletrendszernek nincs megoldása!\n";
                    return;
                }
                k--;

                TextBox[] megoldás = new TextBox[ismeretlen];

                for (int b = 0; b < ismeretlen; i++)
                {

                    megoldás[b] = new TextBox();
                    megoldás[b].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                    megoldás[b].Left = 536+ b * 36;
                    megoldás[b].Top = 36 * b + 10;
                    megoldás[b].Width = 35;
                    megoldás[b].Font = new Font(megoldás[b].Font.FontFamily, 16);
                    megoldás[b].BackColor = Color.Cyan;
                    megoldás[b].TextAlign = HorizontalAlignment.Center;
                    megoldás[b].Text = eredmény[ismeretlen - 1].ToString();
                    this.panel1.Controls.Add(megoldás[b]);

                }
                FileStream fs = new FileStream(file, FileMode.Create);
                BinaryWriter bw = new BinaryWriter(fs);
                for (int r = 0; r < ismeretlen; r++)
                    for (int t = 0; t < ismeretlen + 1; t++)
                        bw.Write(egyenletek[r][t]);

                bw.Close();
                fs.Close();


            }
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {


            numericUpDown1.Maximum = 6;
            numericUpDown1.Minimum = 2;



        }

        private void Generál_Click(object sender, EventArgs e)
        {

        this.panel1.Controls.Clear();


        ismeretlen = (int)numericUpDown1.Value;

        TB = new TextBox[ismeretlen][];

        for(int i = 0; i < ismeretlen; i++)
            TB[i] = new TextBox[ismeretlen + 1];


        int height = 20;
        int width = 40;
        int curX = 10;
        int curY = 10;
        for(int i = 0; i < ismeretlen; i++)
        {
            for(int j = 0; j < ismeretlen + 1; j++)
            {
                TextBox txtbox = new TextBox();
                txtbox = new TextBox();
                txtbox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                txtbox.Left = curX;
                txtbox.Top = curY;
                txtbox.Width = width;
                txtbox.Height = height;
                txtbox.Font = new Font(txtbox.Font.FontFamily, 16);
                txtbox.BackColor = Color.Azure;
                txtbox.TextAlign = HorizontalAlignment.Center;

                TB[i][j] = txtbox;
                this.panel1.Controls.Add(TB[i][j]); // Add as a child of panel

                curX += width + 15;
            }

            curX = 10;
            curY = curY + height + 20;
            }

        }

        private void Ment_Click(object sender, EventArgs e)
        {

        }

    }
}
spupul
  • 23
  • 7
  • Hi! Im making a linear equation solver program in Microsoft Visual c#, this is a semester school project for me. The other half of the code isnt that interesting, i use an algorythm which uses jagged arrays(i already tried it and it works) and i have a button for makeing textboxes dynamically for the n variable and the konstant.(I'll comment it here). The biggest problem is that i dont really understand the error message either because Engilsh isnt my native language. – spupul May 18 '15 at 22:43
  • The error message says "This thing you're pointing at? It doesn't exist, there's nothing there." – rlb.usa May 18 '15 at 22:52
  • Thank you so far! Here is full code [link](https://jsfiddle.net/1fLo0f43/2/) – spupul May 18 '15 at 23:05
  • 1
    Does this help you any? https://www.microsoft.com/hu-hu/download/details.aspx?id=23682 It is a language pack for Visual Studio 2010, so captions appear in Hungarian . – rlb.usa May 18 '15 at 23:23
  • probably the language pack will help you. In general, if it says object reference not set to an instance of an object, you are trying invoke a method on an object which doesn't exist (it is null). Here is a channel 9 video which shows [how to debug with visual studio](http://channel9.msdn.com/Events/TechEd/NorthAmerica/2014/DEV-B352) – Amit May 18 '15 at 23:31
  • Yeah it would help, (sometimes its really difficult to understand the language mixed with technical terms) but sadly the download doesnt starts, I think its bugged. – spupul May 18 '15 at 23:50

1 Answers1

0

In this line of code, you only initialize the first dimension of your array:

int[][] egyenletek = new int[ismeretlen][];

But you then use it before initializing the second dimension a handful of lines later (so this dimension is null (an object not set to a reference)):

egyenletek[a][b] =int.Parse(TB[a][b].Text);

Before that line, you should initialize the 2nd dimension in some way. You did this at another part of your code, in the lines of 172-173 in your jsfiddle link.

In general, when you see this error you should evaluate what objects you are reading from and assigning to and ensure they have been initialized (ie. they are not null).

  • Oh i see. Now i tried to initalize with the same way as I did with the textboxes ( for (int i = 0; i < ismeretlen; i++) egyenletek[i] = new int[ismeretlen + 1];). The error message is gone, now the whole thing froze out. I think its cursed – spupul May 19 '15 at 00:54
  • If you're in Visual Studio, you could try pausing it and see where the code is at at a given time. If you think through your logic following this code, maybe you could simplify it in some way. If my answer is the right one, I think your question would be a duplicate of [this question](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ryan Patterson May 19 '15 at 01:06
  • I got it, that was the primary problem what you said. Thank you. – spupul May 19 '15 at 01:14