Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello,

I want to calculate tax or service tax using c# .net wpf application through textbox textchange event.but my code is getting error when in run the application or this form;-
my code:-

C#
private void txtServiceTax_TextChanged(object sender, TextChangedEventArgs e)
       {
           decimal st, total, nt;
           total = Convert.ToDecimal(txtTotal.Text); \\Error is getting this line
           st = Convert.ToDecimal(txtServiceTax.Text);
           nt = (total * st) / 100;
           txtNetPayBill.Text =Convert.ToString(nt);
       }


NullRefException.. getting error in "total=Convert.ToDecimal(txtTotal.Text);".

Please help me.

Thanks in Advance.

Ankit Agarwal
Software Engineer
Posted
Comments
Sergey Alexandrovich Kryukov 30-Apr-15 2:28am    
Please don't show the code which cannot compile. Use copy/paste, don't type code on the question page.
—SA

The "null exception" in the line you pointed out could be thrown only if txtTotal is null, which is possible, but not so easy to believe. I would advise you to double check it under the debugger. But don't use Convert, use decimal.TryParse, handle the situations when the parsing fails because of wrong input in the text box.

You cannot ask such questions every time you have this exception. It's important to handle such basic problems by yourself. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object".

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx,
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx.

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx.

Good luck,
—SA
 
Share this answer
 
Probably entering wrong decimal separator. Check number format in your culture info.

Or use TryParse:
C#
decimal st, total, nt;
boolean converted;
converted = Decimal.TryParse(txtTotal.Text, total);
if (converted)
// do stuff)
else {
// some message box?
txtTotal.Focus();
return;
}
 
Share this answer
 
use Decimal.TryParse[^]

Code change:

C#
decimal total,st;
 decimal.TryParse(txtTotal.Text, out total);
 decimal.TryParse( txtServiceTax.Text, out st);


for null exception, possible cases are the textbox is cleared somewhere
try to handle null using below code, but some issue in your code only, where the textbox is removed from the DOM
C#
if( txtTotal != null) {
           decimal total,st;
           decimal.TryParse(txtTotal.Text, out total);
           }
 
Share this answer
 
v2
Comments
[no name] 30-Apr-15 2:50am    
Hello Karthik,

Your code is not running.
Same exception NullRefException in decimal.TryParse(txtTotal.Text, out total);

first of all textchange event fired before form load event.
when i am application run.
Karthik_Mahalingam 30-Apr-15 3:02am    
check my updated solution
Karthik_Mahalingam 30-Apr-15 2:59am    
please make sure that txtTotal is not null
[no name] 30-Apr-15 3:03am    
my txt total is null because grid column total it will put in this textBox and after that we will put on service tax on total on textBoxChange event.

If i removed textChange event so its not giving error.

But i have to calculate service tax on total.
Karthik_Mahalingam 30-Apr-15 3:08am    
post the code inside textChange event

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