Best way to deal with a possible null reference exception

I am working with a VB.net piece of code that has been flagged by some auditing software. The auditing software gives this message:
The method readSachelDatabaseFile() in fmrClientDataUpdate.vb can dereference a null pointer on line 60, thereby raising a NullException

How can I fix this code so that it will not be flagged by the audit software? I was thinking of just checking the currentRow object to see if it is null before it is assigned a value. I don't think that is the best solution though. I need some expert help on this.

35 Private Function readSachelDatabaseFile(ByRef filepath As String) As DataTable
36    Try
37      Dim xmldoc As New XmlDocument
38      Dim tblSachelDatabase As New DataTable
39      Dim currentRow As DataRow
40      Dim rootNode As XmlNode
41
42      tblSachelDatabase.Columns.Add("Client",Type.GetType("System.String"))
43      tblSachelDatabase.Columns.Add("Environment", Type.GetType("System.String"))
44      tblSachelDatabase.Columns.Add("SachelServer",Type.GetType("System.String"))
45      tblSachelDatabase.Columns.Add("SachelPort", Type.GetType("System.String"))
46      tblSachelDatabase.Columns.Add("SachelDatabase", Type.GetType("System.String"))
47
48      xmldoc.Load(filepath)
49      rootNode = xmldoc.SelectSingleNode("//Clients")
50        For Each clientnode As XmlNode In rootNode.ChildNodes        
51          For Each environmentnode As XmlNode In clientnode.ChildNodes
52            currentRow = tblSachelDatabase.NewRow
53            For Each childnode As XmlNode In environmentnode.ChildNodes
54              currentRow(childnode.Name) = childnode.InnerText
55            Next
56              currentRow("Client") = clientnode.Attributes("name").Value
57              currentRow("Environment") = environmentnode.Attributes("name").Value
58              tblSachelDatabase.Rows.Add(currentRow)
59          Next
60         Next
61         tblSachelDatabase.AcceptChanges()
62         Return tblSachelDatabase
63      Catch ex As Exception
64          throw ex
65      End try
66      End Function
LVL 2
brgdotnetcontractorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Is it flagging a specific line?

I doubt your "filepath" parameter being passed into the function needs to be "ByRef"...change that to "ByVal".
brgdotnetcontractorAuthor Commented:
It's flagging line 60. I changed the ByRef to ByVal for the filpath, however I am still getting the same audit message.
Jacques Bourgeois (James Burger)PresidentCommented:
In line 49, if the //Clients node does not exists, rootNode will be Nothing (null).

Your auditing software might detect that possibility. Although I would expect to get a NullReferenceException when referencing the variable on line 50, not when dereferencing it on 60. But who knows. Code analysis sometimes has its own ways of seeing things.

You might try testing for Nothing before entering the loop:

                 If rootNode IsNot Nothing Then
                        For Each clientnode As XmlNode In rootNode.ChildNodes
                              For Each environmentnode As XmlNode In clientnode.ChildNodes
                                    currentRow = tblSachelDatabase.NewRow
                                    For Each childnode As XmlNode In environmentnode.ChildNodes
                                          currentRow(childnode.Name) = childnode.InnerText
                                    Next
                                    currentRow("Client") = clientnode.Attributes("name").Value
                                    currentRow("Environment") = environmentnode.Attributes("name").Value
                                    tblSachelDatabase.Rows.Add(currentRow)
                              Next
                        Next
                 End If

Not knowing your software, I cannot say if it will detect that check or not.

Some auditing software also have the possibility of disabling a specific message on a given line, through different ways. If you are sure that your files will always have the //Clients node, you might look to see if your software has that feature and disable the message for that line.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
brgdotnetcontractorAuthor Commented:
Thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.