问题描述
您好,
我想通过textbox textchange事件使用c#.net wpf应用程序计算税或服务税。但我的代码在运行时遇到错误申请表或此表格; -
我的代码: -
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:-
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 ..在total = Convert.ToDecimal(txtTotal.Text);中获取错误。 />
请帮帮我。
先谢谢。
Ankit Agarwal
软件工程师
NullRefException.. getting error in "total=Convert.ToDecimal(txtTotal.Text);".
Please help me.
Thanks in Advance.
Ankit Agarwal
Software Engineer
推荐答案
decimal st, total, nt;
boolean converted;
converted = Decimal.TryParse(txtTotal.Text, total);
if (converted)
// do stuff)
else {
// some message box?
txtTotal.Focus();
return;
}
decimal total,st;
decimal.TryParse(txtTotal.Text, out total);
decimal.TryParse( txtServiceTax.Text, out st);
为空异常,可能的情况是文本框被清除了某处
尝试使用下面的代码处理null,但是你的一些问题仅限代码,其中文本框已从DOM中删除
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
if( txtTotal != null) {
decimal total,st;
decimal.TryParse(txtTotal.Text, out total);
}
这篇关于如何在我的代码中处理空引用异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!