Results 1 to 6 of 6

Thread: Object reference not set to an instance of an object Loading Form

Hybrid View

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Object reference not set to an instance of an object Loading Form

    i copied the following form the StackTrace line of the exception error. I am recieving this error when I click on a menustrip item in Form1 to load Form4. Form4 displays some pictures in PictureBoxes, I have verified that the pictures exist.

    If I click Ok on the error message in runtime then attempt to show Form4 again the form displays however without anything in PictureBoxes

    If anyone has any idea on what I should focus on I would greatly appreciate it

    at WindowsApplication1.Form4.Form4_Load(Object sender, EventArgs e) in D:\Documents and Settings\bmoran\Desktop\MyStuff\smartCalc PRO TextFieldParser USB\Form4.vb:line 5
    at System.Windows.Forms.Form.OnLoad(EventArgs e)
    at System.Windows.Forms.Form.OnCreateControl()
    at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
    at System.Windows.Forms.Control.CreateControl()
    at System.Windows.Forms.Control.WmShowWindow(Message& m)
    at System.Windows.Forms.Control.WndProc(Message& m)
    at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
    at System.Windows.Forms.ContainerControl.WndProc(Message& m)
    at System.Windows.Forms.Form.WmShowWindow(Message& m)
    at System.Windows.Forms.Form.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.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
    at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
    at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
    at System.Windows.Forms.Control.Show()
    at WindowsApplication1.Subject_Values.SubjectPhotos1ToolStripMenuItem_Click(Object sender, EventArgs e) in D:\Documents and Settings\bmoran\Desktop\MyStuff\smartCalc PRO TextFieldParser USB\Form1.vb:line 2146

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Object reference not set to an instance of an object Loading Form

    well... I'd say \Form4.vb:line 5 would be a good spot to start...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Object reference not set to an instance of an object Loading Form

    The problem always has the same solution: Find the item that is Nothing and figure out why it is nothing. Normally this is done by examining the line you are taken to when the exception occurs, and examining each object to see which one is Nothing. However, you may have found one of the odd cases where this approach won't work. If the error is happening when you try to load form4, then the line where the exception appears will probably be something like Form4.Show, which is not where the problem is occurring. Instead, the problem is that there is something happening when form4 is created. Since that looks like a default instance, that seems even more likely. Therefore, the true problem is either in the constructor for Form4, or something called by the constructor, or in the initialization of a form level variable for Form4. While this does make it harder to track down, it also suggests some approaches.

    First off, put a breakpoint on the InitializeComponents call in Form4. If execution reaches this point without the exception happening, then the problem is after that. More likely, execution won't reach this point, in which case the problem is with the initialization of a form level variable.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Object reference not set to an instance of an object Loading Form

    Thanks guys it appears the problem is in loading the form and images being in place. I dont think my code is very code for the loading of the form

    PictureBox1.ErrorImage.Tag = "Err"

    If Me.PictureBox1.Image.Tag IsNot "Err" Then
    Me.PictureBox1.ImageLocation = (String.Format("{0}\{1}.jpg", Subject_Values.ToolStripStatusLabel3.Text, "Subject_Front"))
    Me.PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage

    End If
    I am not verse in PictureBoxes at all and admitantly got this code from another site, it owrked for quite sometime so i never dove into it. With that said if I dim out as such the form loads fine with the correct images.

    Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'PictureBox1.ErrorImage.Tag = "Err"

    'If Me.PictureBox1.Image.Tag IsNot "Err" Then
    Me.PictureBox1.ImageLocation = (String.Format("{0}\{1}.jpg", Subject_Values.ToolStripStatusLabel3.Text, "Subject_Front"))
    Me.PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage

    'End If
    I wanted the form to be able to load even if there wasnt an appropriate picture and display the err message in the picture box

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Object reference not set to an instance of an object Loading Form

    Well it's pretty obvious why it doesn't work in the original!

    vb.net Code:
    1. PictureBox1.ErrorImage.Tag = "Err"
    2.  
    3. If Me.PictureBox1.Image.Tag IsNot "Err" Then ' how can it NOT be "Err". You just set it to "Err"!!!!!
    4. Me.PictureBox1.ImageLocation = (String.Format("{0}\{1}.jpg", Subject_Values.ToolStripStatusLabel3.Text, "Subject_Front"))
    5. Me.PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
    6.  
    7. End If
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Object reference not set to an instance of an object Loading Form

    also... isnot is used for OBJECT comparison... not string... it should be <> it either = to something or <> to something (reads it is either equal to something or not equal to something)

    the isnot is used when comparing objects... as in

    if PictureBox1 isNot Picturebox ...

    makes me wonder if option explicit & option strict is turned on

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width