c# - Object reference not set to an instance of an object Error in List -
this question has answer here:
- what nullreferenceexception, , how fix it? 33 answers
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
Post a Comment