c# - Object reference not set to an instance of an object Error in List -


this question has answer here:

return error "object reference not set instance of object error" when trying add item in list. in other words it's return error in addtonote.add(name); , sure name not null.

protected void features(string name, string shortname) { list<string> addtonote =  viewstate["note"] list<string> ;     if (name.length > 0)     {         addtonote.add(name);     } else     {for (int x = 0; x < addtonote.count; x++)         {if (addtonote[x].startswith(shortname))             {                 addtonote.removeat(x);}         } }     viewstate["note"] = addtonote;     txtnote.text = string.join(txtnote.text , ",", addtonote); } protected void chkpersonalaccedent_checkedchanged(object sender, eventargs e) {if (chkpersonalaccedent.checked == true)     { features("حوادث شخصية", "حوادث شخصية");}     else      { features("", "حوادث شخصية"); }} 

if viewstate["note"] not list<string>, expression

viewstate["note"] list<string>; 

will return null. addtonote null , why runtime exception thrown.


Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -