Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am trying to create dynamic PictureBox checking and Image filling. it should go over all controls and check is it PictureBox or not. When found, doing check if pcount variable is bigger than 0. another check is trying to determine name of PictureBox. If match found, passing to it's Image property image name.

What am i missing ?

error: 'An unhandled exception of type 'System.NullReferenceException' occurred"

C#
int pcount = ppM.Count;
   foreach (Control pbox in this.Controls)
   {
    if (pbox is PictureBox)
    {
     if (pcount <= 0) break;
     if (pbox.Name.StartsWith("PicB" + pcount))
     {
      pcount--;
      ((PictureBox)pbox).Image = ppM[pcount].img ;
     }
    }
   }

pcount = 2;

static way works:

C#
PicB1.Image = ppM[0].img;
PicB2.Image = ppM[1].img;

Thanks in advance for any suggestions;
Posted
Comments
Sergey Alexandrovich Kryukov 18-Aug-14 17:59pm    
In what line?
—SA

You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx[^],
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx[^].

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].

Good luck,
—SA
 
Share this answer
 
In addition to Solution 1: it's the absolute wrong idea to search anything by name. This property does not affect any functionality, is only generated for the designer infrastructure. No application code should ever rely on this property.

Your designer generated you the variable declaration, the reference to the PictureBox. Use it. The search code makes no sense at all. If you have several picture boxes to work with, create, for example an array of them.

—SA
 
Share this answer
 
v2
Comments
Nelek 20-Aug-14 17:47pm    
OP answered you in a non-solution below. Content is (just in case it gets nuked):
Wow,

Thank you for so wast explenation and your time Sergey.


error comes up at line " ((PictureBox)pbox).Image = ppM[pcount].img ;"

as background of this code i can tell that on my form there are four PicterBoxes with names
PicB1, PicB1, PicB3 and PicB4, other PictureBoxes names but there are not only one controls on that form.

I was trying to fill them dependantly of pcount value.
I am new to debug think but thanks to your tips i've found root cause of an error using breakpoint - "Autos" window. Before i was just using try/catch to show messagebox(or debug output) with an error value but that was waste of time as i see.

Doing changes i hardcoded some parameters and that was my error.

Thank you Sergey and have a nice Day!

regards
Sergey Alexandrovich Kryukov 20-Aug-14 23:27pm    
Thank you, Nelek.
—SA
Nelek 21-Aug-14 4:03am    
You are welcome

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