0

i know what the problem means but i cant seems to find the null pointer , my code has two forms :

form 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 :

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 ?

pquest
  • 3,151
  • 3
  • 27
  • 40
  • 1
    It's possible that some of the values in `name` are still null so calling `Equals` will fail. If you instead use `name[i] == meals[i]` it will work, or at least not throw an exception. – juharr Apr 09 '20 at 19:03
  • 1
    On what line is the error occurring? – Jasper Kent Apr 09 '20 at 19:04
  • 1
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Heretic Monkey Apr 09 '20 at 19:08
  • @juharr thank you man , it actually worked but can you tell me the deference between Equals and == ? – anas alassi Apr 09 '20 at 19:27
  • @anasalassi `Equals` is a instance method that cannot be called on a null reference where as the equality operator acts more like a static method that takes two values and thus can take a null value and it handles that case. – juharr Apr 09 '20 at 19:54

1 Answers1

0

While you initialized the array with 9 items in it, it would initialize them to null. If you do name[5].Equals and name[5] is null, you will get the crash. 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.

Daniel Lorenz
  • 4,178
  • 1
  • 32
  • 39