Click here to Skip to main content
15,913,758 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this code runs an exception "object reference is set to an instance of an object" what's wrong in this code?can any one help me?

x = 40;
for (int i = 1; i <=can_Count; i++)
{
  bu1[index1] = new Button();
  bu1[index1].Name = "BI" + i;
  bu1[index1].Text = "1";
  bu1[index1].Location = new Point(20, x);
  bu1[index1].ForeColor = Color.Goldenrod;
  bu1[index1].BackColor = Color.Black;
  bu1[index1].Size = new Size(72, 47);
  x = x + 75;
  index1++;
  try
  {
   bu1[i].Click += new EventHandler(button1_Click);
  }
  catch (Exception ee)
  { MessageBox.Show(ee.Message); }
}
this.Controls.AddRange(bu1);


[edit]Code block added, "Ignore HTML..." option disabled - OriginalGriff[/edit]
Posted
Updated 14-Jun-11 21:10pm
v3
Comments
Member 7779792 15-Jun-11 3:35am    
thank you every one!appreciate your help a lot!!
i haven't initialize the index1 variable!!!
how stupid i am!!!!
thank u once again! :))))

Probably index1 and i are out of sync (I cannot say that for sure because you didn't show index1 initialization value) hence bu1[i] is not set to an object reference.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Jun-11 14:55pm    
My 5. Also pay attention -- misuse of exception.
Please see my answer.
--SA
Firstly, what is the value of index1? Why are you appearing to use it in conjuction with i to reference the same element of bu1? Are they in step?

Why are you using can_Count as your loop termination condition, but not checking that this is lower or equal to bu1.Length?

I am guessing that the error occurs on the line
bu1[index1] = new Button();
which would indicate the index1 is already outside the bound of bu1.

Put a breakpoint on the line
bu1[index1] = new Button();
Then run your program. Work out in your head what the various variables values should be, then check them. If they are ok, single step through repeating the checks until you spot the error. If they aren't what you think they should be, find out why not!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Jun-11 14:56pm    
My 5.
Also pay attention -- misuse of exception. Please see my solution.
--SA
I suppose the problem is in bu1[ i ] - you use index1 in most places and then i in event handler attachment. i starts from 1, index1 most likely from 0. So you are actually trying to access next button which is not created yet.
Possible solutions:
1) use bu1[index1].Click += ...
2) bu1[i - 1 ].Click += .... (not sure about this one, depends on initial index1 value)
 
Share this answer
 
change this

bu1[index1].Click += new EventHandler(button1_Click);
 
Share this answer
 
In addition to other answers: your try-catch block makes no sense.

First, you probably think that you're catching some exceptions in this block if they are thrown in the handler. Hardly. You only try to catch exception on the operation of adding a handler to the invocation list of your event, which hardly would throw any exception.

More importantly, you're abusing exception mechanism by over-trying. Except for rare exclusions, you need to catch all exceptions in only one point of the code per thread. In case of UI, you need to catch them in a main cycle of UI. It could be event handler of the event System.Windows.Forms.Application.OnThreadException (Forms) or System.Windows.Application.DispatcherUnhandledException (WPF).

See:
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.application.aspx[^].

For more directions on use of exceptions, see my past answers:
How do i make a loop that will stop when a scrollbar reaches the bottom[^],
When i run an application an exception is caught how to handle this?[^],
throw . .then ... rethrowing[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900