473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

NullReferenceException

1
""An unhandled exception of type 'System.NullReferenceException' occurred in WindowsFormsApplication2.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.performclick operation, I do get this problem. I clearly do not see any problems in my code but please take a look in it.


Expand|Select|Wrap|Line Numbers
  1.  {
  2.  
  3.                 Button z = null;
  4.  
  5.                 z = find_win_or_block("O");
  6.  
  7.                 if (z == null){
  8.  
  9.                     find_win_or_block("X"); if (z == null){
  10.  
  11.                         find_corner();
  12.                         if (z == null){
  13.  
  14.                             find_open();
  15.  
  16.                         }
  17.                     }
  18.                 }
  19.                z.PerformClick();
  20.  
  21.  
  22.  
  23.  
  24.        }
  25.         public Button find_win_or_block(string mark)
  26.         {
  27.             Console.WriteLine("Find win or block " + mark);
  28.             if (a1.Text == mark && a2.Text == mark && a3.Text == "")
  29.                 return a3;
  30.             if (a2.Text == mark && a3.Text == mark && a1.Text == "")
  31.                 return a1;
  32.             if (a3.Text == mark && a1.Text == mark && a2.Text == "")
  33.                 return a2;
  34.  
  35.             if (b1.Text == mark && b2.Text == mark && b3.Text == "")
  36.                 return b3;
  37.             if (b1.Text == mark && b3.Text == mark && b2.Text == "")
  38.                 return b2;
  39.             if (b2.Text == mark && b3.Text == mark && b3.Text == "")
  40.                 return b1;
  41.  
  42.             if (c1.Text == mark && c2.Text == mark && c3.Text == "")
  43.                 return c3;
  44.             if (c2.Text == mark && c3.Text == mark && c1.Text == "")
  45.                 return c1;
  46.             if (c2.Text == mark && c1.Text == mark && c3.Text == "")
  47.                 return c3;
  48.             //8=======D
  49.             if (a1.Text == mark && b1.Text == mark && c1.Text == "")
  50.                 return c1;
  51.             if (a1.Text == mark && c1.Text == mark && b1.Text == "")
  52.                 return b1;
  53.             if (b1.Text == mark && c1.Text == mark && a1.Text == "")
  54.                 return a1;
  55.  
  56.             if (b2.Text == mark && a2.Text == mark && c2.Text == "")
  57.                 return c2;
  58.             if (c3.Text == mark && b2.Text == mark && a2.Text == "")
  59.                 return a2;
  60.             if (a2.Text == mark && c2.Text == mark && b2.Text == "")
  61.                 return b2;
  62.  
  63.             if (c3.Text == mark && b3.Text == mark && a3.Text == "")
  64.                 return a3;
  65.             if (a3.Text == mark && c3.Text == mark && b3.Text == "")
  66.                 return b3;
  67.             if (a3.Text == mark && b3.Text == mark && c3.Text == "")
  68.                 return c3;
  69.             //OIOIOIOIOIOIOIOIXXXIOIOIOIOIOIOIOIOIO
  70.             if (a1.Text == mark && b2.Text == mark && c3.Text == "")
  71.                 return c3;
  72.             if (a1.Text == mark && c3.Text == mark && b2.Text == "")
  73.                 return b2;
  74.             if (b2.Text == mark && c3.Text == mark && a1.Text == "")
  75.                 return a1;
  76.  
  77.             if (a3.Text == mark && b2.Text == mark && c1.Text == "")
  78.                 return c1;
  79.             if (b2.Text == mark && c1.Text == mark && a3.Text == "")
  80.                 return a3;
  81.             if (c1.Text == mark && a3.Text == mark && b2.Text == "")
  82.                 return a3;
  83.  
  84.             return null;
  85.  
  86.         }
  87.         public Button find_corner()
  88.         {
  89.             if (a1.Text == "O")
  90.             {
  91.                 if (a3.Text == "")
  92.                     return a3;
  93.                 if (c3.Text == "")
  94.                     return c3;
  95.                 if (c1.Text == "")
  96.                     return c1;
  97.             }
  98.             if (a3.Text == "O")
  99.             {
  100.                 if (a1.Text == "")
  101.                     return a1;
  102.                 if (c3.Text == "")
  103.                     return c3;
  104.                 if (c1.Text == "")
  105.                     return c1;
  106.             }
  107.             if (c1.Text == "O")
  108.             {
  109.                 if (a3.Text == "")
  110.                     return a3;
  111.                 if (c3.Text == "")
  112.                     return c3;
  113.                 if (a1.Text == "")
  114.                     return a1;
  115.             }
  116.             if (c3.Text == "O")
  117.             {
  118.                 if (a3.Text == "")
  119.                     return a3;
  120.                 if (a1.Text == "")
  121.                     return a1;
  122.                 if (c1.Text == "")
  123.                     return c1;
  124.             }
  125.             if (a3.Text == "")
  126.                 return a3;
  127.             if (c3.Text == "")
  128.                 return c3;
  129.             if (c1.Text == "")
  130.                 return c1;
  131.             if (a1.Text == "")
  132.                 return a1;
  133.  
  134.             return null;
  135.         }
  136.         public Button find_open()
  137.         {Console.WriteLine("Find open ");
  138.             Button qwerty = null;
  139.             foreach(Control a in Controls)
  140.             {
  141.                 qwerty = a as Button;
  142.                   if(qwerty!=null)
  143.                 {
  144.                     if(qwerty.Text=="")
  145.                     return qwerty;
  146.                 }
  147.             }
  148.             return null;
  149.             }
Jan 9 '16 #1
1 1571
zmbd
5,501 Expert Mod 4TB
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.
Jan 9 '16 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Terrence | last post by:
I am doing some of the C# walkthroughs to transition from VB to C#. When I try to execute static void Main() { Aplication.Run(new Form1()) } I raise a 'System.NullReferenceException" in...
5
by: Fabio Papa | last post by:
Hi, I am fairly new to programming and and even newer to dotnet. I appoligise in advance if this is a dumb questions, and I would appreciate if you could answer it anyways. :) I am writing a...
1
by: Rafael | last post by:
Hi, I hope I can find some help for this problem IDE: Visual Studio.NET 2003 Developer Editio Language: C# Problem: "An unhandled exception of type 'System.NullReferenceException' occurred in...
5
by: TT (Tom Tempelaere) | last post by:
Hi, Once in a while my application throws an NullReferenceException at startup, however it appears to be occurring in an unknown module. If I debug it and ask to 'break', then there is no source...
2
by: Enrico Pangan | last post by:
I'm trying to call some functions in a C++ Dll, "Library.dll" from C#. Some functions work but some return the NullReferenceException. I have here the source code for the C++ version and for the...
2
by: Raed Sawalha | last post by:
i have a windows form(Main) with listview, when click an item in listview i open other window form (Sub) which generate the selected item from parent window in as treeview items when click any item...
1
by: msnews.microsoft.com | last post by:
I'm trying to fill an array of objects but when I add the first object I get a NullReferenceException. ----------------------------------------------------------------------------...
6
by: William Mild | last post by:
I must be getting brain fried. I can't see the error. Create a new web form with the following code begind: Public Class test Inherits System.Web.UI.Page Public Class ReportCardData ...
9
by: Xero | last post by:
could anybody describes a scenario where this error will occur? i have visited the page about the NullReferenceException in msdn library but i still have no idea what it is about. thanks. ...
3
by: Sagar | last post by:
Hi. I am working on a project to migrate a web application from 1.1 to 2.0 Within in the DAL of the application, there is a call to below function that builds a command object for later use. ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.