0

I am trying to make a little program in C# just for practice, but I've encountered a problem that I can't find a solution to anywhere.

Program has 2 textboxes(Ime1 and Dug1) and a button, so when I press the button it creates an object(duznik1) like this:

 Duznik duznik1 = new Duznik(Ime1.Text, int.Parse(Dug1.Text));

This is the Duznik class:

public class Duznik
    {
        public int count = 0;
        public string ime;
        public int dug;
        TextBox[] Imena;
        TextBox[] Dugovi;
        TextBox[] Vraceno;
        TextBox[] Dodato;


        public Duznik(string imeDuznika, int kolikoDuguje)
        {
            ime = imeDuznika;
            dug = kolikoDuguje;


            Imena[count] = new TextBox();
            Imena[count].Text = imeDuznika;

            Dugovi[count] = new TextBox();
            Dugovi[count].Text = dug.ToString();

            Vraceno[count] = new TextBox();
            Dodato[count] = new TextBox();

            count++;

        }
    }

So basically, what I want it to do is create TextBox arrays(Imena,Dugovi,Vraceno,Dodato), and populate these arrays with textboxes(which I try to make in the constructor method), but whatever I do, i always get an error at the line "Imena[count].Text = imeDuznika;", and the error says:

An unhandled exception of type 'System.NullReferenceException' occurred in Dugovi.exe

Additional information: Object reference not set to an instance of an object.

I tried to comment out that line, but whatever I do, the same error appears in the constructor method, just on a different line. I am new to programming so any help is welcome, thanks in advance :)

hpdobrica
  • 134
  • 7

2 Answers2

3

In this line

Imena[count] = new TextBox();

you try to set the index 0 of the array to an instance of TextBox, but you haven't defined anywhere the size of the array. So this is basically just a variable that point to nothing in memory and trying to set something at offset zero of this variable triggers the NullReferenceException. It is like writing

null[0] = new TextBox();

You need to define somewhere your array to contain a defined number of textboxes

// An array to hold 10 textboxes
TextBox[] Imena = new TextBox[10];

Notice that, after this, the array can contains 10 textboxes but it is still empty and you need to assign the textbox to the correct offset.

Using an array has numerous impractical disadvantages. The most important is that you need to know the maximum dimension and if you reach that limit you need to reallocate everything for a new maximum dimension. It is better to use a more flexible collection object

List<TextBox> Imena = new List<TextBox>();

Imena.Add(new TextBox);

Here you don't need to define a max limit and you don't waste memory if your user need a small quantity of TextBox

As a side note. Creating the TextBox is not enough to have them showed. You need to add them to a control container (usually a form but also GroupBox or Panel) and set a position relative to the container coordinates

TextBox t1 = new List<TextBox>();
t1.Location = new Point(10,10);
formContainerInstance.Controls.Add(t1);
Steve
  • 213,761
  • 22
  • 232
  • 286
  • Thanks, this worked like a charm :) no error now, but textbox I created doesnt seem to appear anywhere. Is that because I need to specify it's location or for some other reasons? – hpdobrica Jan 11 '14 at 17:05
  • You need to specify Location at minimum (and size if needed) but most important of all. You need to add the TextBox to the Controls collection of the Form that hosts them on video – Steve Jan 11 '14 at 17:07
1

You need to initialize the array first with the size you want. Its throwing exception because you didn't define the size of the Array.

TextBox[] Imena = new TextBox[4]();

Other option is to use a list instead so that you don't have to define the size.

List<TextBox> Imena = new List<TextBox>();

Check below to read when to use Array vs List.

Array versus List<T>: When to use which?

Community
  • 1
  • 1
Adarsh Shah
  • 6,755
  • 2
  • 25
  • 39