Click here to Skip to main content
15,909,896 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralObject null reference exception Pin
ademsandeepreddy10-Feb-10 3:08
ademsandeepreddy10-Feb-10 3:08 
GeneralRe: Object null reference exception Pin
dan!sh 10-Feb-10 3:53
professional dan!sh 10-Feb-10 3:53 
GeneralRe: Object null reference exception Pin
William Winner10-Feb-10 6:03
William Winner10-Feb-10 6:03 
GeneralRe: Object null reference exception Pin
ademsandeepreddy10-Feb-10 6:48
ademsandeepreddy10-Feb-10 6:48 
GeneralRe: Object null reference exception Pin
rhuiden10-Feb-10 6:25
rhuiden10-Feb-10 6:25 
GeneralRe: Object null reference exception Pin
William Winner10-Feb-10 6:59
William Winner10-Feb-10 6:59 
GeneralRe: Object null reference exception Pin
rhuiden10-Feb-10 7:21
rhuiden10-Feb-10 7:21 
RantRe: Object null reference exception Pin
William Winner10-Feb-10 6:17
William Winner10-Feb-10 6:17 
I gotta say, this is badly written. First, why would you put an entire sub within a Try/Catch block? If you want the old VB6 On Error, you can still use it...though it's not suggested.

Only put the sections of code that you think could throw an exception within the Try/Catch. That's what it's there for.
VB
Private Sub populateTreeview()
Try
* 'Just a good practice -- change the cursor to a
* 'wait cursor while the nodes populate
* Me.Cursor = Cursors.WaitCursor
* 'First, we'll load the Xml document
*  Dim xDoc As New XmlDocument()
  xDoc.Load(SelectTextFile())
* 'Now, clear out the treeview,
* 'and add the first (root) node
*  tvXmlTree.Nodes.Clear()
  tvXmlTree.Nodes.Add(New TreeNode(xDoc.DocumentElement.Name))
*  Dim tNode As New TreeNode()
*  tNode = DirectCast(tvXmlTree.Nodes(0), TreeNode)
* 'We make a call to addTreeNode,
* 'where we'll add all of our nodes
*  addTreeNode(xDoc.DocumentElement, tNode)
* 'Expand the treeview to show all nodes
*  tvXmlTree.CollapseAll()
Catch xExc As XmlException
  'Exception is thrown is there is an error in the Xml
  MessageBox.Show(xExc.Message)
Catch ex As Exception
  'General exception
  MessageBox.Show(ex.Message)
Finally
  'Change the cursor back
  Me.Cursor = Cursors.[Default]
End Try
End Sub


I've put asterisks besides the lines of code that should never throw an exception. So, there are only two lines that could possible throw an error...if the SelectFile() doesn't return a valid item for the xDoc.Load and if xDoc.DocumentElement doesn't exist, though if it doesn't it means that the xDoc.Load didn't work which should have already thrown an exception.

And, by the way,
VB
DirectCast(tvXmlTree.Nodes(0), TreeNode)

in this line, all TreeView Nodes are TreeNodes. You don't need to DirectCast them.
QuestionFilter between date Pin
akosidandan9-Feb-10 21:09
akosidandan9-Feb-10 21:09 
AnswerRe: Filter between date Pin
thatraja9-Feb-10 22:07
professionalthatraja9-Feb-10 22:07 
GeneralRe: Filter between date Pin
akosidandan10-Feb-10 2:12
akosidandan10-Feb-10 2:12 
AnswerRe: Filter between date Pin
thatraja10-Feb-10 2:16
professionalthatraja10-Feb-10 2:16 
AnswerRe: Filter between date Pin
εїзεїзεїз10-Feb-10 1:20
εїзεїзεїз10-Feb-10 1:20 
QuestionHow to add app.config for Visual Basic Application Pin
ramdili9-Feb-10 4:42
ramdili9-Feb-10 4:42 
AnswerRe: How to add app.config for Visual Basic Application Pin
Dave Kreskowiak9-Feb-10 5:38
mveDave Kreskowiak9-Feb-10 5:38 
AnswerRe: How to add app.config for Visual Basic Application Pin
εїзεїзεїз10-Feb-10 1:28
εїзεїзεїз10-Feb-10 1:28 
Questionvb6 to vb.net conversion problem in excel Pin
D.Manivelan8-Feb-10 20:12
D.Manivelan8-Feb-10 20:12 
AnswerRe: vb6 to vb.net conversion problem in excel Pin
TheComputerMan9-Feb-10 1:05
TheComputerMan9-Feb-10 1:05 
AnswerRe: vb6 to vb.net conversion problem in excel Pin
loyal ginger9-Feb-10 3:06
loyal ginger9-Feb-10 3:06 
AnswerRe: vb6 to vb.net conversion problem in excel Pin
DaveAuld10-Feb-10 1:46
professionalDaveAuld10-Feb-10 1:46 
QuestionHow to get the path of Send To Pin
Anubhava Dimri8-Feb-10 19:54
Anubhava Dimri8-Feb-10 19:54 
AnswerRe: How to get the path of Send To Pin
DaveAuld8-Feb-10 20:13
professionalDaveAuld8-Feb-10 20:13 
GeneralRe: How to get the path of Send To Pin
Anubhava Dimri8-Feb-10 20:37
Anubhava Dimri8-Feb-10 20:37 
GeneralRe: How to get the path of Send To Pin
DaveAuld8-Feb-10 20:43
professionalDaveAuld8-Feb-10 20:43 
GeneralRe: How to get the path of Send To Pin
TheComputerMan9-Feb-10 1:09
TheComputerMan9-Feb-10 1:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.