Note that there are some explanatory texts on larger screens.

plurals
  1. POobject reference not set to an instance of object exception when I try to add element to an array of type List<string>
    primarykey
    data
    text
    <p>In my project I've got a variable numtorust which represents the number of the toursits. (for testing purposes numtourist = 2)</p> <pre><code>for (i=0,numtoursut; i++) </code></pre> <p>I create dynamically 5 checkboxes with assigned checkedChanged event for each of tourist. Also to keep track which checkbox to which tourist applies I add attribute 'collection' </p> <pre><code>mycheckbox.InputAttributes.Add("collection", i.ToString()); </code></pre> <p>In checkedchanged event handler - when a user selects a checkbox I checked if its collection attribute is = 0 or 1 (first or second user). then I add checkbox value to <code>myche1</code> which is of type <code>List&lt;string)</code> if collection attribute = 1. </p> <p>But I when I decided to make one <strong>array</strong> of type <code>List&lt;string&gt;</code> with name <code>Toursit</code> when I try to add an element to it I got an exception - Object reference not set <strong>to an instance of object</strong> in this row of my code</p> <pre><code>Toursist[Int32.Parse(chk.InputAttributes["collection"])].Add(chk.InputAttributes["value"].ToString()); </code></pre> <p>Here is my full code</p> <pre><code> protected void checkChanged(object sender, EventArgs e) { CheckBox chk = (CheckBox)sender; /*that doesn't work if (chk.Checked) { Toursist[Int32.Parse(chk.InputAttributes["collection"])].Add(chk.InputAttributes["value"].ToString()); ((List&lt;String&gt;[])Session["chk"])[Int32.Parse(chk.InputAttributes["collection"])] = Toursist[Int32.Parse(chk.InputAttributes["collection"])]; }*/ //this works with myche1 of type list&lt;string&gt; if ((chk.Checked)&amp;&amp;(chk.InputAttributes["collection"].Equals("1"))) { myche1.Add(chk.InputAttributes["value"].ToString()); lblProba.Text += chk.InputAttributes["value"].ToString(); Session["chk1"] = myche1; } } </code></pre> <p>edit 1:</p> <p>teh new code of </p> <p>protected void checkChanged(object sender, EventArgs e) {</p> <pre><code>List&lt;string&gt;[] Toursist = new List&lt;string&gt;[2]; //Session["chk"] = new List&lt;string&gt;[2]; for (int i = 0; i &lt; Toursist.Length; i++) { Toursist[i] = new List&lt;string&gt;(); // ((List&lt;String&gt;[])Session["chk"])[i] = Toursist[i]; } CheckBox chk = (CheckBox)sender; if (chk.Checked) { if (((List&lt;String&gt;[])Session["chk"])[Int32.Parse(chk.InputAttributes["collection"])] == null) { ((List&lt;String&gt;[])Session["chk"])[Int32.Parse(chk.InputAttributes["collection"])] = Toursist[Int32.Parse(chk.InputAttributes["collection"])]; } Toursist[Int32.Parse(chk.InputAttributes["collection"])].Add(chk.InputAttributes["value"].ToString()); lblProba.Text += chk.InputAttributes["collection"].ToString(); ((List&lt;String&gt;[])Session["chk"])[Int32.Parse(chk.InputAttributes["collection"])] = Toursist[Int32.Parse(chk.InputAttributes["collection"])]; } </code></pre> <p>again the same mistake this time when I test if Sessio["chk"] == 0.</p> <p>but If I uncomment (so I no more have this mistake) </p> <pre><code>// ((List&lt;String&gt;[])Session["chk"])[i] = Toursist[i]; </code></pre> <p>on each postback event my session will be empty and I don't want!! </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload