""An unhandled exception of type 'System.NullRef erenceException ' occurred in WindowsFormsApp lication2.exe
Additional information: Object reference not set to an instance of an object."
I am new at programming and I don't really get the explanation that I saw on the sites.
When I am going to use a Button.performc lick operation, I do get this problem. I clearly do not see any problems in my code but please take a look in it.
Additional information: Object reference not set to an instance of an object."
I am new at programming and I don't really get the explanation that I saw on the sites.
When I am going to use a Button.performc lick operation, I do get this problem. I clearly do not see any problems in my code but please take a look in it.
Code:
{
Button z = null;
z = find_win_or_block("O");
if (z == null){
find_win_or_block("X"); if (z == null){
find_corner();
if (z == null){
find_open();
}
}
}
z.PerformClick();
}
public Button find_win_or_block(string mark)
{
Console.WriteLine("Find win or block " + mark);
if (a1.Text == mark && a2.Text == mark && a3.Text == "")
return a3;
if (a2.Text == mark && a3.Text == mark && a1.Text == "")
return a1;
if (a3.Text == mark && a1.Text == mark && a2.Text == "")
return a2;
if (b1.Text == mark && b2.Text == mark && b3.Text == "")
return b3;
if (b1.Text == mark && b3.Text == mark && b2.Text == "")
return b2;
if (b2.Text == mark && b3.Text == mark && b3.Text == "")
return b1;
if (c1.Text == mark && c2.Text == mark && c3.Text == "")
return c3;
if (c2.Text == mark && c3.Text == mark && c1.Text == "")
return c1;
if (c2.Text == mark && c1.Text == mark && c3.Text == "")
return c3;
//8=======D
if (a1.Text == mark && b1.Text == mark && c1.Text == "")
return c1;
if (a1.Text == mark && c1.Text == mark && b1.Text == "")
return b1;
if (b1.Text == mark && c1.Text == mark && a1.Text == "")
return a1;
if (b2.Text == mark && a2.Text == mark && c2.Text == "")
return c2;
if (c3.Text == mark && b2.Text == mark && a2.Text == "")
return a2;
if (a2.Text == mark && c2.Text == mark && b2.Text == "")
return b2;
if (c3.Text == mark && b3.Text == mark && a3.Text == "")
return a3;
if (a3.Text == mark && c3.Text == mark && b3.Text == "")
return b3;
if (a3.Text == mark && b3.Text == mark && c3.Text == "")
return c3;
//OIOIOIOIOIOIOIOIXXXIOIOIOIOIOIOIOIOIO
if (a1.Text == mark && b2.Text == mark && c3.Text == "")
return c3;
if (a1.Text == mark && c3.Text == mark && b2.Text == "")
return b2;
if (b2.Text == mark && c3.Text == mark && a1.Text == "")
return a1;
if (a3.Text == mark && b2.Text == mark && c1.Text == "")
return c1;
if (b2.Text == mark && c1.Text == mark && a3.Text == "")
return a3;
if (c1.Text == mark && a3.Text == mark && b2.Text == "")
return a3;
return null;
}
public Button find_corner()
{
if (a1.Text == "O")
{
if (a3.Text == "")
return a3;
if (c3.Text == "")
return c3;
if (c1.Text == "")
return c1;
}
if (a3.Text == "O")
{
if (a1.Text == "")
return a1;
if (c3.Text == "")
return c3;
if (c1.Text == "")
return c1;
}
if (c1.Text == "O")
{
if (a3.Text == "")
return a3;
if (c3.Text == "")
return c3;
if (a1.Text == "")
return a1;
}
if (c3.Text == "O")
{
if (a3.Text == "")
return a3;
if (a1.Text == "")
return a1;
if (c1.Text == "")
return c1;
}
if (a3.Text == "")
return a3;
if (c3.Text == "")
return c3;
if (c1.Text == "")
return c1;
if (a1.Text == "")
return a1;
return null;
}
public Button find_open()
{Console.WriteLine("Find open ");
Button qwerty = null;
foreach(Control a in Controls)
{
qwerty = a as Button;
if(qwerty!=null)
{
if(qwerty.Text=="")
return qwerty;
}
}
return null;
}
Comment