我有一些代码,当它执行时,它抛出一个NullReferenceException,说:
你调用的对象是空的.
这是什么意思,我该怎么做才能解决这个错误?
关于这个.NET未处理的异常消息:
你调用的对象是空的.
为什么.NET不显示哪个对象null?
我知道我可以检查null并解决错误.但是,为什么.NET没有帮助指出哪个对象具有空引用以及哪个表达式触发了NullReferenceException?
我运行程序时不断收到此错误.
你调用的对象是空的.描述:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例.
Source Error:
Line with error:
Line 156: if (strSearch == "" || strSearch.Trim().Length == 0)
Run Code Online (Sandbox Code Playgroud)
它的正确写法是什么?
我有以下操作方法async和await关键字:
[HttpPost]
public async Task<ActionResult> Save(ContactFormViewModel contactFormVM)
{
if (domain.SaveContactForm(contactFormVM) > 0)// saves data in database
{
bool result = await SendMails(contactFormVM);//need to execute this method asynchronously but it executes synchronously
return Json("success");
}
return Json("failure");
}
public async Task<bool> SendMails(ContactFormViewModel contactFormVM)
{
await Task.Delay(0);//how to use await keyword in this function?
domain.SendContactFormUserMail(contactFormVM);
domain.SendContactFormAdminMail(contactFormVM);
return true;
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,一旦数据库操作完成,我想立即返回Json()结果然后调用SendMails()应该在后台执行的方法.我应该对上面的代码做出哪些更改?
我在这一行得到了对象引用错误:emp [count] .emp_id = int.Parse(parts [0]);
在这段代码中
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public class employees
{
public int emp_id;
public string firstName;
public string lastName;
public double balance;
}
private void btnOpen_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
DialogResult result = file.ShowDialog();
if (result == DialogResult.Cancel) return;
string fileName = file.FileName;
StreamReader reader = new StreamReader(fileName);
string[] lines = File.ReadAllLines(fileName);
int emp_count = lines.Count<string>();
employees[] emp = new employees[emp_count]; …Run Code Online (Sandbox Code Playgroud)