NullReferenceException

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hloQt
    New Member
    • Jan 2016
    • 1

    NullReferenceException

    ""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.


    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;
                }
    Last edited by zmbd; Jan 9 '16, 04:03 PM. Reason: [z{please format script/tables using the [CODE/] tool}]
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    Your posted code has close to 150 lines, expecting someone to just step thru it is a bit much without some further details:

    >> What have you already done to troubleshoot this script?

    >> Have you stepped thru the script using the debugger?
    MS Visual Studio: Navigating through Code with the Debugger - at what point do you receive the error?

    >> How are you creating your project? Visual Studio, Visual Studio Express or some other ISE and what version?)

    This information will help our experts to pinpoint the problem.

    Comment

    Working...