0

I have to make an application in c# as a windows form.I chose to do a Lee Search between 2 points chosen by user and through walls put by user,and also to colour the path.

Here's is my project http://www.girlshare.ro/34729013. What it really pisses me off is the fact that sometimes this type of error "Object reference not set to an instance of an object" appears when nothing is wrong with my code.

For example in form1.cs, at newsearch() function I declare a matrix of buttons and initialize it.At some time even that code caused object reference error,but now works,don't ask me why,I just rewrited it(ever if it was the same as before).

The problem is in function Lee_Click where I want to reproduce the path and color it.You can see that there is an if (if (a[i,j]!=null)).I know that a[i,j] is null beacuse when I run the program,it doesn't color the path.

What is wrong with it?I initialized the matrix of buttons....And maybe you could give me some advice about this error,I had it many times,but with luck or idk I managed to solve it.

pzaenger
  • 11,381
  • 3
  • 45
  • 46
  • 2
    "object reference not set to an instance of an object" - this is one of the most common code errors. It basically means that the variable you think has something in, doesn't have anything in. – user230910 May 03 '15 at 11:30
  • Yes,this is the problem. I already initalized the matrix of buttons,then why the matrix is null?Also,when I place the buttons in the main panel,I place a[i,j],so somehow in the function NewSearch(),a[i,j] is not null,but in other functions it is:)) I must have said that the matrix is declared global. – Baltatu Andrei Mircea May 03 '15 at 11:32
  • my guess is, context - probably the variable is declared and filled in context a and you are trying to use it in context b (another thread, another form, another new() something etc.. – user230910 May 03 '15 at 11:41
  • Since the variable is glocal in form1.cs, theoretically it is not a problem. What is weird is the fact that ,how I said earlier,I solved this problem sometimes just rewriting the code.Maybe it is my computer the problem... – Baltatu Andrei Mircea May 03 '15 at 11:47
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – H H May 03 '15 at 12:06
  • We need a sample of your initialization code and to see that if (a[i,j] != null) code in context. I'm not downloading your code and digging through it. – James McLachlan May 03 '15 at 12:16

0 Answers0