|
-
Nov 13th, 2012, 02:46 PM
#1
Thread Starter
Frenzied Member
Debugging Unhandled Exceptions
I have a "App Window" that I have been using for some months now. Every once in awhile I get a unhandled exception with the below text
Code:
************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
at AppWindow.Form1.Timer2_Tick(Object sender, EventArgs e)
at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Is there something that anybody sees that I shouldn't be doing or a problem in the code? My problem is I don't know where the error is exactly.
Here is the function
Code:
Private Sub Timer2_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Dim curNode As TreeNode = Me.tvFolders.SelectedNode
If curNode.ToString.Length = 0 Then
curNode = tvFolders.Nodes("Jackson County")
End If
Dim SelectNode As String = curNode.Name
Try
' check to see if file exist, if so do refresh then delete file
If (File.Exists(appPath & "refresh.ini")) Then
File.Delete(appPath & "refresh.ini")
Me.Update()
' this clears icons,
lvFiles.Items.Clear()
FillTreeView()
InitializeTimer()
'MsgBox("1 " & SelectNode)
If curNode.Text = "Personal" And My.Settings.Favorites.Count = 0 Then
tvFolders.Nodes.Remove(tvFolders.Nodes("Personal"))
curNode = Nothing
End If
'Me.tvFolders.SelectedNode = curNode
If curNode.ToString.Length > 0 Then
If curNode.Text = "Personal" And My.Settings.Favorites.Count > 0 Then
SelectNode = appPath & favorites(0)
End If
Dim theNode() As TreeNode = tvFolders.Nodes.Find(SelectNode, True)
'MsgBox("node length:" & theNode.Length)
If theNode.Length > 0 Then
'MsgBox("2 " & theNode(0).Text)
Me.tvFolders.SelectedNode = theNode(0)
End If
Else
'MsgBox("curnode is blank")
Me.tvFolders.SelectedNode = tvFolders.Nodes("Jackson County")
End If
'MsgBox(Me.tvFolders.SelectedNode)
objFS.ListFoldersFiles(Me.tvFolders.SelectedNode.Tag, lvFiles, ImageList2)
'MsgBox(curNode.Text)
Timer2.Enabled = False
Cursor.Current = System.Windows.Forms.Cursors.Default
ToolStripStatusLabel1.Text = ""
Me.tvFolders.Focus()
tvFolders.LabelEdit = False
Else
Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
InitializeTimerRefresh()
End If
Catch ex As Exception
Debug.WriteLine(ex.Message)
MsgBox("Refresh Failed" & Chr(13) & Chr(13) & ex.Message, vbOKOnly, "Failed To Refresh Timer")
End Try
End Sub
-
Nov 13th, 2012, 02:56 PM
#2
Re: Debugging Unhandled Exceptions
You might consider adding unhandled exception handler to your application which write the exception to a log file to assist in determining the issue then once know write assertion to address the issue generating the exception.
MSDN article which will assist with adding an unhandled exception handler that writes to a log file.
-
Nov 13th, 2012, 03:11 PM
#3
Re: Debugging Unhandled Exceptions
open your project... press Ctrl+Alt+E .... might take a moment, but that will open the Exceptions configuration dialog... you're interested in the second one... "Common Language Runtime Exceptions" ... make sure BOTH checkboxes are marked on that one... for both "Thrown" and "User-unhandled" ...
Run your app... it will now stop on the line that is throwing the error. Some object on that line is Nothing... you'll need to inspect each object in the line to see which one it is.
-tg
-
Nov 13th, 2012, 03:16 PM
#4
Re: Debugging Unhandled Exceptions
Good point from techgnome but if this is at runtime on a user's computer you still need a method to collect the exception information other than showing the standard dialog .NET shows as at least for me user's generally are not to be relied on to remember the message. Best to write the message to a file or have the user do a screen snap shot.
-
Nov 13th, 2012, 04:35 PM
#5
Re: Debugging Unhandled Exceptions
By the way, since the method in question is mostly wrapped in an exception trap, it is quite likely that the exception occurs prior to the trap, which means that it is happening in these lines:
Code:
Dim curNode As TreeNode = Me.tvFolders.SelectedNode
If curNode.ToString.Length = 0 Then
curNode = tvFolders.Nodes("Jackson County")
End If
Dim SelectNode As String = curNode.Name
So, do you see where a problem could exist in this code? The second line will throw that exception if curNode Is Nothing, which it will be if there is no selectedNode, since TreeView.SelectedNode will return Nothing if no node is selected.
However, that's not the only problem with that code. Supposing that there IS a node, then the If statement will run. If there is no "Jackson County" node, then curNode should end up set to Nothing, again, which would mean that the line immediately after the If statement will also throw that exception.
One of those two issues is likely to be the problem, and both of them could potentially be a problem, though the latter might not exist in practice.
My usual boring signature: Nothing
 
-
Nov 13th, 2012, 05:12 PM
#6
Thread Starter
Frenzied Member
Re: Debugging Unhandled Exceptions
thank guys, much appreciated.
Kevin, why can't I click on your link? it underlines like a link but won't allow me to click. and I agree, we can't reply on the user, lol. And I have never been able to produce this in testing or debug mode.
Shaggy,
I am not sure I follow. How can this line throw an exception
Code:
If curNode.ToString.Length = 0 Then
If it is 0? even if there is a node selected why would it even run that if statement? I mean it will run but it won't be true, correct? My idea is to see if a node is selected, by default there should be one selected at all times. But if nothing is selected than I need it to be the tree parent, which is Jackson County. what is the best way to check without throwing an exception?
-
Nov 13th, 2012, 05:49 PM
#7
Re: Debugging Unhandled Exceptions
ok.... looks like we need to break this down a bit...
Code:
Dim curNode As TreeNode = Me.tvFolders.SelectedNode
If curNode.ToString.Length = 0 Then
curNode = tvFolders.Nodes("Jackson County")
End If
Dim SelectNode As String = curNode.Name
That's the code snip in question...
This line gets the selected node:
Dim curNode As TreeNode = Me.tvFolders.SelectedNode
BUT... what if there IS no node selected... curNode becomes Nothing ...
Next line:
If curNode.ToString.Length = 0 Then
if curNode is nothing... you CANNOT call the toString method... because for all intents and purposes it simply DOES NOT EXIST... it is void... empty, ziltch. A puff of smoke would have more substance.
And that is the crux of what Shaggy is trying to say... if curNode is nothing, you can't call .ToString on it, and that's the kind of thing that causes the Null Reference error you're seeing.
So you need to check for that... something along these lines is appropriate:
Code:
Dim curNode As TreeNode = Me.tvFolders.SelectedNode
If (curNode isnot Nothing) andalso (curNode.ToString.Length = 0) Then
curNode = tvFolders.Nodes("Jackson County")
End If
Dim SelectNode As String = curNode.Name
so what I've done there is to check to first make sure that curNode is something if it is, then and only then will it process the rest of the if statement (because I used AndAlso rather than just And which would evaluate everythign first... giving us the same problem we're trying to avoid)
-tg
-
Nov 13th, 2012, 06:11 PM
#8
Thread Starter
Frenzied Member
Re: Debugging Unhandled Exceptions
ahhh, thanks techgnome, that puts it into perspective. I was understanding that you could call ToString anytime. good to know thank you.
I will make the appropriate changes and test it out. I also found Kevin's " unhandled exception handler that writes to a log file." page so I will look into that as well.
-
Nov 13th, 2012, 06:45 PM
#9
Re: Debugging Unhandled Exceptions
.ToString is a method of type Object. ALL variables in .NET derive from Object. Therefore, you can call .ToString against any object. However, Nothing is not an object. It is the absence of an object, and the absence of an object certainly has no members, so it certainly can't have a method.
My usual boring signature: Nothing
 
-
Nov 14th, 2012, 07:28 AM
#10
Re: Debugging Unhandled Exceptions
 Originally Posted by phpman
thank guys, much appreciated.
Kevin, why can't I click on your link? it underlines like a link but won't allow me to click. and I agree, we can't reply on the user, lol. And I have never been able to produce this in testing or debug mode.
Not sure why it failed, try this one
http://code.msdn.microsoft.com/Deali...ndled-9b933818
-
Nov 14th, 2012, 08:05 AM
#11
Re: Debugging Unhandled Exceptions
the link didn't work because some how it came out as "dealing%20with%20unhandled%20exceptions%20in%20window%27s%20form%20solution" nicely url encoded, but it wasn't a url... *shrug*
-tg
-
Nov 14th, 2012, 09:14 AM
#12
Re: Debugging Unhandled Exceptions
 Originally Posted by techgnome
the link didn't work because some how it came out as "dealing%20with%20unhandled%20exceptions%20in%20window%27s%20form%20solution" nicely url encoded, but it wasn't a url... *shrug*
-tg
I am not surprised, we all have seen stranger things happen.
-
Nov 14th, 2012, 11:38 AM
#13
Re: Debugging Unhandled Exceptions
[ot]Ain't that the truth... true story: we had a piece of code that was failing in an app I was writing back in Vb4 days... couldn't figure it out... so I added MsgBoxes around the suspect code ... it began working... so I took the MsgBoxes out... it would break... I put them back in, and it works... so I simply commented out the MsgBox lines... and the code ran fine... took the comments out... and it breaks... WTH? so I put the comments back in and shipped it. Two years later it was still running. Another time, years later, similar situation... the solution ended up being two lines of code: Dim x as Integer ... X = 1 ... go figure
[/ot]
-tg
-
Nov 14th, 2012, 01:30 PM
#14
Re: Debugging Unhandled Exceptions
 Originally Posted by techgnome
[ot]Ain't that the truth... true story: we had a piece of code that was failing in an app I was writing back in Vb4 days... couldn't figure it out... so I added MsgBoxes around the suspect code ... it began working... so I took the MsgBoxes out... it would break... I put them back in, and it works... so I simply commented out the MsgBox lines... and the code ran fine... took the comments out... and it breaks... WTH? so I put the comments back in and shipped it. Two years later it was still running. Another time, years later, similar situation... the solution ended up being two lines of code: Dim x as Integer ... X = 1 ... go figure
[/ot]
-tg
We had a similar issue (forget which version it was of VB classic). Very frustrating that something works with MsgBox (for debugging) and breaks if it's not there.
-
Nov 14th, 2012, 05:49 PM
#15
Re: Debugging Unhandled Exceptions
It was one of those things where you suddenly expect to hear Rod Serling's voice over "A developer... working alone at night. Solving problems no one realizes is there. But little does he know, his code has just compiled into.... The Twilight Zone..."
-tg
-
Nov 16th, 2012, 04:35 PM
#16
Thread Starter
Frenzied Member
Re: Debugging Unhandled Exceptions
lol, eerie, I can totally see that in the Twilight Zone.
Kevin, I am trying to get this logging working and I was confused at first but I then I used your ApplicationEvents.vb and added some xml references, but I still get errors
Code:
Warning 1 Variable declaration without an 'As' clause; type of Object assumed. H:\Visual Studio 2010\Projects\DirectoryTest\ApplicationEvents.vb 81 21 AppWindowTest
Error 2 Range variable 'F' hides a variable in an enclosing block or a range variable previously defined in the query expression. H:\Visual Studio 2010\Projects\DirectoryTest\ApplicationEvents.vb 81 38 AppWindowTest
Error 3 Expression of type 'System.Collections.Generic.IEnumerable(Of System.Xml.Linq.XElement)' is not queryable. Make sure you are not missing an assembly reference and/or namespace import for the LINQ provider. H:\Visual Studio 2010\Projects\DirectoryTest\ApplicationEvents.vb 81 43 AppWindowTest
Warning 4 Variable declaration without an 'As' clause; type of Object assumed. H:\Visual Studio 2010\Projects\DirectoryTest\ApplicationEvents.vb 99 17 AppWindowTest
Error 5 'System.Collections.Generic.IEnumerable(Of System.Xml.Linq.XElement)' cannot be indexed because it has no default property. H:\Visual Studio 2010\Projects\DirectoryTest\ApplicationEvents.vb 99 27 AppWindowTest
Warning 6 Variable declaration without an 'As' clause; type of Object assumed. H:\Visual Studio 2010\Projects\DirectoryTest\ExceptionDialog.vb 36 13 AppWindowTest
Error 7 Expression of type '1-dimensional array of String' is not queryable. Make sure you are not missing an assembly reference and/or namespace import for the LINQ provider. H:\Visual Studio 2010\Projects\DirectoryTest\ExceptionDialog.vb 55 38 AppWindowTest
I actually followed this to get my ApplicationEvents.vb going
http://msdn.microsoft.com/en-us/library/f9shkfdd.aspx
Last edited by phpman; Nov 16th, 2012 at 04:43 PM.
-
Nov 16th, 2012, 05:32 PM
#17
Thread Starter
Frenzied Member
Re: Debugging Unhandled Exceptions
Nevermind, I fixed all the errors. Now just to see if I can get it to run, lol
Last edited by phpman; Nov 16th, 2012 at 05:41 PM.
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
|