将文本框设置为属性会抛出null ref异常 [英] setting textbox to property throw null ref exception
问题描述
我有一个表格,其中有一个文本框
< asp:textbox id =txtbox1runat =server>
在我设置的asp.net页面后面的代码中
class1.class2.class2txt1 = txtbox1.text。 trim();
public class1
{
private int _val ;
private class2 _class2val;
public int VAl
{
get {return this._val;}
set {this._val = value;}
}
public class2 Class2Val
{
get {return this._class2val;}
set {this._class2val = value;}
}
公共等级2
{
私有字符串_txtvalue = string.empty;
公共字符串class2txt1
{
get {return this._txtvalue;}
set { this._txtvalue = value;}
}
}
Im relati非常新的.net ...请帮助我在回发时获得null引用异常。
I have a form in which there is a textbox
<asp:textbox id="txtbox1" runat="server">
in the code behind i set of the asp.net page
class1.class2.class2txt1= txtbox1.text.trim();
public class1
{
private int _val;
private class2 _class2val;
public int VAl
{
get {return this._val;}
set {this._val = value;}
}
public class2 Class2Val
{
get {return this._class2val;}
set {this._class2val= value;}
}
public class2
{
private string _txtvalue=string.empty;
public string class2txt1
{
get {return this._txtvalue;}
set {this._txtvalue= value;}
}
}
Im relatively new to .net ...please help as to why am i getting a nullreference exception when i postback .
推荐答案
txtbox1.text。修剪()
;如果文本为null,则抛出一个null异常。
你不应该直接修剪函数。
试试这个 -
if(txtbox1.text!= null)= class1.class2.class2txt1 = txtbox1.text.trim();
还有其他选择。
例如Convert.ToString()
可用于避免此类错误。
txtbox1.text.trim()
; will throw a null exception if the text is null.
You should not trim a function directly.
Try this -
if (txtbox1.text!= null) = class1.class2.class2txt1= txtbox1.text.trim();
There are other alternatives as well.
For e.g.Convert.ToString()
which can be used to avoid this kind of error.
空引用异常是由于未使用new用于在文件后面的aspx代码中初始化class2。当我照顾它时它就消失了。
感谢您的回复。
The null reference exception was due to not using the new to initialize class2 in aspx code behind file. When I took care of that it went away.
Thanks for being so kind and replying.
这篇关于将文本框设置为属性会抛出null ref异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!