Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 .
Posted
Comments
Sergey Alexandrovich Kryukov 15-Aug-14 0:23am    
In what line?
—SA
Member 10562713 15-Aug-14 3:35am    
get {return this._txtvalue;}
Sergey Alexandrovich Kryukov 15-Aug-14 3:57am    
Why are you writing code which won't compile?
No, it cannot be this line, because "this" is never null. It must be in the line which calls this getter.
You already got the answer.
—SA

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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Aug-14 0:25am    
Abhinav,

This is C#. Why would you repeat the OP's lame code which won't even compile, because capitalization is wrong?
(Well-spotted though.)

—SA
Abhinav S 15-Aug-14 0:30am    
Capitalization is for the OP to sort out.
Sergey Alexandrovich Kryukov 15-Aug-14 1:15am    
Maybe, but what is some naive readers get confused? Please, we should use every chance to propagate culture and avoid confusing the readers; many of the are already confused too much. If you ignored mistakes of others, I would understand, but writing such thing yourself? Remember, the reputation score you earned may create considerable trust in the readers, it should impose some responsibility...

I hope you will understand me. Isn't it too hard to correct it?

Thank you.
—SA
Abhinav S 15-Aug-14 3:17am    
Case is something that the compiler will indicate by giving an error. I do not know why that should confuse the user.
The whole idea is to give it a try and solve the actual problem - without worrying too much about the details of case-sensitivity.

If the user had indeed made a mistake with case, then obviously, he has to be pointed in the right direction.

In questions like this one, case will not be important. What will be important is try to solve the real problem (which in this case was the null error).
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900