[英]How to Diagnose Object Reference not set to an Instance of an Object
i know what the problem means but i cant seems to find the null pointer, my code has two forms:我知道问题的含义,但我似乎找不到 null 指针,我的代码有两个 forms:
form 1:表格 1:
public static String [] name = new String[9];
private void button4_Click(object sender, EventArgs e)
{
if (label1.Text.Equals("Big Mac®"))
{
name[0] = "Big Mac®";
}
if (label1.Text.Equals("Little Mac™"))
{
name[1] = "Little Mac™";
}
if (label1.Text.Equals("Artisan Grilled Chicken "))
{
name[2] = "Artisan Grilled Chicken ";
}
if (label1.Text.Equals("Double Cheeseburger"))
{
name[3] = "Double Cheeseburger";
}
if (label1.Text.Equals("Double Quarter"))
{
name[4] = "Double Quarter";
}
if (label1.Text.Equals("Quarter Pounder® "))
{
name[5] = "Quarter Pounder® ";
}
if (label1.Text.Equals("Filet-O-Fish®"))
{
name[6] = "Filet-O-Fish®";
}
if (label1.Text.Equals("Artisan Grilled Chicken"))
{
name[7] = "Artisan Grilled Chicken";
}
if (label1.Text.Equals("McChicken®"))
{
name[8] = "McChicken®";
}
this.Hide();
}
form 2:表格 2:
for (int i = 0; i < Form5.name.Length; i++)
{
if (Form5.name[i].Equals(meals[i])) {//something is pointing to null
label10.Text += meals[i] +"\n";
label13.Text += Convert.ToString(Form5.price[i]+" Jd"+"\n");
label18.Text += Convert.ToString(Form5.count+"\n");
label21.Text += Convert.ToString(Form5.Total+ " Jd"+"\n");
resault[i] = Form5.Total + (Form5.Total*0.16);
}
sum += resault[i];
}
label11.Text = Convert.ToString(sum);
is there any null pointer in this code?此代码中是否有任何 null 指针?
While you initialized the array with 9 items in it, it would initialize them to null.当您初始化包含 9 个项目的数组时,它会将它们初始化为 null。 If you do name[5].Equals and name[5] is null, you will get the crash.
如果您执行 name[5].Equals 并且 name[5] 为 null,您将遇到崩溃。 The better check to do is this:
更好的检查是这样的:
if (string.Equals(Form5.name[i], meals[i]))
However, if both are null, this will return true - not sure if you want that to happen or not.但是,如果两者都是 null,这将返回 true - 不确定您是否希望发生这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.