|
-
Sep 19th, 2012, 03:27 PM
#1
Thread Starter
Frenzied Member
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
-
Sep 19th, 2012, 03:35 PM
#2
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
-
Sep 19th, 2012, 03:48 PM
#3
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
 
-
Sep 19th, 2012, 05:09 PM
#4
Thread Starter
Frenzied Member
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
-
Sep 19th, 2012, 05:17 PM
#5
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:
PictureBox1.ErrorImage.Tag = "Err"
If Me.PictureBox1.Image.Tag IsNot "Err" Then ' how can it NOT be "Err". You just set it to "Err"!!!!!
Me.PictureBox1.ImageLocation = (String.Format("{0}\{1}.jpg", Subject_Values.ToolStripStatusLabel3.Text, "Subject_Front"))
Me.PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
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!
-
Sep 19th, 2012, 07:01 PM
#6
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|