locked
Array of pictureboxes

    Question

  • Well I try to create an array of pictureboxes but I get this error:


    System.NullReferenceException was unhandled
      Message=Object reference not set to an instance of an object.
      Source=SockTest
      StackTrace:
           at frmMain.button1_Click(Object sender, EventArgs e) in c:\Documents and Settings\TitanX\My Documents\Visual Studio 2008\SockTest\SockTest\frmMain.cs:line 193
           at System.Windows.Forms.Control.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
           at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.ButtonBase.WndProc(Message& m)
           at System.Windows.Forms.Button.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.Run(Form mainForm)
           at frmMain.Main() in c:\Documents and Settings\TitanX\My Documents\Visual Studio 2008\SockTest\SockTest\frmMain.cs:line 169
           at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException: 
    



            PictureBox[] rocimages = new PictureBox[50];
    
            rocimages = new PictureBox[50];
    
    
            for (int i = 0; i < 51; i++)
            {
    
                rocimages[i].Image = (System.Drawing.Image)ROCreq("image", "?hash=" + RandomString(30, true), "/img.php");
                progressBar1.Value += 1;
    
    
            }
    Does somebody know how I can solve it?
    Friday, September 11, 2009 1:45 PM

Answers

  • rocimages[i] = new PictureBox;

    Don't forget to add it to the Controls collection of it's container.

    And you're iterating through 1 more PictureBox than you have.  Change the test to 1 less.
    • Edited by JohnWein Friday, September 11, 2009 5:33 PM
    • Marked as answer by Harry Zhu Monday, September 21, 2009 9:05 AM
    Friday, September 11, 2009 4:08 PM

  • progressBar1.Value++;  // can throw an exception if attempt to set Value exceeds Maximum property.
    This is safer.

    progressBar1.Step = 1;  // set this prior to use
    //
    progressBar.PerformStep();  // cannot throw an exception at all.

    Rudedog   =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by Harry Zhu Monday, September 21, 2009 9:05 AM
    Friday, September 11, 2009 5:12 PM
  • You forgot to initialize the array elements:

      for (int ix = 0; ix < rocImages.Length; ++ix) rocImages[ix] = new PictureBox();

    Your next problem is an IndexOutOfRange exception when your for loop tries to set the Image property on the 51st picture box.  C# starts counting from 0, not 1.  The best thing to do is to use the length property as I've shown above.

    Hans Passant.
    • Marked as answer by Harry Zhu Monday, September 21, 2009 9:05 AM
    Saturday, September 12, 2009 9:27 PM
    Moderator

All replies

  • try :
    PictureBox[50] rocimages;
    Also progressBar1.Value++; might look nicer!

    http://twitter.com/graymalkinsblog http://practicalhardware.blogspot.com/
    Friday, September 11, 2009 4:07 PM
  • rocimages[i] = new PictureBox;

    Don't forget to add it to the Controls collection of it's container.

    And you're iterating through 1 more PictureBox than you have.  Change the test to 1 less.
    • Edited by JohnWein Friday, September 11, 2009 5:33 PM
    • Marked as answer by Harry Zhu Monday, September 21, 2009 9:05 AM
    Friday, September 11, 2009 4:08 PM

  • progressBar1.Value++;  // can throw an exception if attempt to set Value exceeds Maximum property.
    This is safer.

    progressBar1.Step = 1;  // set this prior to use
    //
    progressBar.PerformStep();  // cannot throw an exception at all.

    Rudedog   =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by Harry Zhu Monday, September 21, 2009 9:05 AM
    Friday, September 11, 2009 5:12 PM
  • You forgot to initialize the array elements:

      for (int ix = 0; ix < rocImages.Length; ++ix) rocImages[ix] = new PictureBox();

    Your next problem is an IndexOutOfRange exception when your for loop tries to set the Image property on the 51st picture box.  C# starts counting from 0, not 1.  The best thing to do is to use the length property as I've shown above.

    Hans Passant.
    • Marked as answer by Harry Zhu Monday, September 21, 2009 9:05 AM
    Saturday, September 12, 2009 9:27 PM
    Moderator