title: Where to set variables so that they aren't null on postback?
body: <p>My webpage lets a user select one or more tools from a list of available tools (by dragging from the "available" list to the "selected" list). There is a <code>HiddenTools</code> hidden field that is updated every time the two lists change- the <code>&lt;li&gt;</code> in the <code>ToolsActive</code> list are stored as a comma separated list of ids in the <code>HiddenTools</code>. When the user clicks "Save", the value in the <code>HiddenTools</code> is stored in the database.</p>
  
  <p>aspx page:  </p>
  
  <pre><code>&lt;asp:HiddenField runat="server" ID="HiddenTools"/&gt;
  
  /*&lt;li&gt; can be dragged from one list to the other. Every time the lists 
  change, HiddenTools gets updated with the contents of ToolsSelected  
  list, formatted as a comma separated string */
  
  &lt;ul id="ToolsAvailable"&gt;            
      &lt;%foreach (KeyValuePair&lt;int,string&gt; tool in unusedTools){ %&gt;
          &lt;li id='&lt;%= tool.Key %&gt;'&gt; &lt;%= tool.Value %&gt; &lt;/li&gt;
      &lt;% } %&gt;
  &lt;/ul&gt;
  
  &lt;ul id="ToolsActive"&gt;
       &lt;%foreach (KeyValuePair&lt;int,string&gt; aTool in selectedTools){ %&gt;
           &lt;li id='&lt;%= aTool.Key %&gt;'&gt; &lt;%= tool.Value %&gt; &lt;/li&gt;
       &lt;% } %&gt;
   &lt;/ul&gt;
  
  &lt;asp:Button ID="btnSave" OnClick="btnSave_Click" runat="server" Text="Save"/&gt;
  </code></pre>
  
  <p>code-behind:</p>
  
  <pre><code>public partial class Settings
  {
      protected ToolPreferences prefs;
  
      protected Dictionary&lt;int, string&gt; tools;
      protected Dictionary&lt;int, string&gt; unusedTools;
      protected Dictionary&lt;int, string&gt; selectedTools;
  
      protected void Page_Load(object sender, EventArgs e)
      {
           int AccountId = getAccountId();
           if(!Page.IsPostBack){
               prefs = new ToolPreferences(AccountId);
               PopulateTools();
           }
      }
  
      private void PopulateTools()
      {
          tools = getPossibleTools();
          unusedTools = new Dictionary&lt;int, string&gt;();
          selectedTools = new Dictionary&lt;int, string&gt;();
  
          List&lt;int&gt; selectedList = new List&lt;int&gt;();
          if (!string.IsNullOrEmpty(prefs.Tools))
          {
              selectedList = prefs.Tools.Split(',').Select(int.Parse).ToList();
          }
          foreach (KeyValuePair&lt;int, string&gt; aTool in tools)
          {
              if (selectedList.Contains(aTool.Key))
              {
                  selectedTools.Add(aTool.Key, aTool.Value);
              }
              else
              {
                  unusedTools.Add(aTool.Key, aTool.Value);
              }
          }
      }
  
      protected void btnSavePreferences_Click(object sender, EventArgs e)
      {
          ToolPreferences tp = ToolPreferences (AccountId);
          tp.Update(HiddenTools.Value);
      }        
  
  }
  </code></pre>
  
  <p>The issue is that after PostBack, the following error appears:  </p>
  
  <blockquote>
    <p><i>Object reference not set to an instance of an object.</i></p>
  </blockquote>
  
  <p>with the following line highlighted:</p>
  
  <blockquote>
    <p><code>&lt;%foreach (KeyValuePair&lt;int,string&gt; tool in unusedTools){ %&gt;</code></p>
  </blockquote>
  
  <p>If I move the following two lines out of the <code>!PageIsPostBack</code>check, I don't get that error when the page reloads after postback, but I also don't see the changes the user made to the tools lists until the page is reloaded another time.</p>
  
  <pre><code>prefs = new ToolPreferences(AccountId);
  PopulateTools();
  </code></pre>
  
  <p><strong>Where can I set the tools variables so that the "Object reference not set" error doesn't occur?</strong></p>