1

We are trying to read information from a separate XML document.

This is the code we have:

'load the XMLTest document containing cars.
    Dim xml As XDocument = XDocument.Load("CreditApp.xml")


    Dim SSN As String = txtSSN.Text
    'get all car makes that are red.
    Dim query = From xe In xml.Descendants("SSN")
         Where xe.Element("SSN").Value = SSN
                Select New With {
                    .FName = xe.Element("FName").Value 'Error in code is here
                }

    'loop through query result output results.
    For Each element In query.ToArray
        MessageBox.Show(element.FName)
    Next

We are getting the error:

Object reference not set to an instance of an object.

We are not sure what it is refering too.

Robert
  • 4,084
  • 8
  • 39
  • 81
  • 1
    It means you are getting a "NULL". – Shankar Damodaran Apr 12 '12 at 22:00
  • We are trying to match the SSN value we enter to the same value in the XML document. Then populate the other tags based on that set of data. How do we achieve this. – Robert Apr 12 '12 at 22:02
  • What line number of your code does the error message point to? You are attempting to access a property of a `null` value. For example, maybe `xe.Element("FName")` doesn't match any result, so when you say `.Value` it gives the exception. – mellamokb Apr 12 '12 at 22:02
  • 1
    Have you stepped through the code in the debugger? I'd ensure your query actually has a result, and check `txtSSN.Text` as well. – Tim Apr 12 '12 at 22:02
  • possible duplicate of [object reference not set to an instance of object](http://stackoverflow.com/questions/131053/object-reference-not-set-to-an-instance-of-object) – Dan J Apr 12 '12 at 23:02
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Mr Anderson Apr 22 '19 at 21:19

1 Answers1

3

When you seed that message it means that you tried to access a member of a variable that currently has the value of Nothing.

Dim obj As Object = Nothing
Console.WriteLine(obj.ToString()) ' Null ref on obj access

The value Nothing in VB.Net (similar to C# null) means the object has no value. Attempting to access a member on it is accessing a member on nothing. The runtime can't satisfy this request and hence throws an exception to let you know about the problem.

In this particular sample it looks like the most likely cause of the error is the following line

FName = xe.Element("FName").Value

This code doesn't do anything to validate that xe.Element("FName") doesn't return Nothing and hence can lead to an exception when accessing the member Value. To fix this you need to guard against this possibility. The easiest way is to use a helper method

Public Function GetFNameValueOrEmpty(ByVal elem As XElement) As String
  Dim child = elem.Element("FName")
  If child Is Nothing Then
    Return String.Empty
  Else
    Return child.Value
  End If
End Function

With this helper you could rewrite the original query as such

Dim query = From xe In xml.Descendants("SSN")
         Where xe.Element("SSN").Value = SSN
                Select New With {
                    .FName = GetFNameValueOrEmpty(xe)
                }
JaredPar
  • 673,544
  • 139
  • 1,186
  • 1,421
  • When we implement the above code we get an error Option strict on requires all function, property, and operation declarations to have an 'as' clause – Robert Apr 12 '12 at 22:13
  • @user512915 my bad, added the `As String` to the `Function` – JaredPar Apr 12 '12 at 22:21
  • 1
    Add `As String` to the function header: `Public Function GetFNameValueOrEmpty(Dim elem As XElement) As String` – Olivier Jacot-Descombes Apr 12 '12 at 22:21
  • I am now getting two errors on this. 1. FName = GetFNameValueOrEmpty(xe) errors as 'Declaration Expected' on 'FName'. 2. Public Function GetFNameValueOrEmpty(Dim elem As XElement) As String errors as 'Specifiers only valid at the beginning of the declaration' on the 'Dim'. – Robert Apr 12 '12 at 22:34
  • @user512915 sorry, that line was meant to be the replacement for the equivalent line in your original sample. It's not meant to stand on it's own like that – JaredPar Apr 12 '12 at 22:40
  • Sorry, I'm not sure where you are referencing? – Robert Apr 12 '12 at 22:45
  • @user512915 i updated my answer to have the updated code sample – JaredPar Apr 12 '12 at 22:50
  • We are getting an error under the (Dim elem As XElement) As String – Robert Apr 12 '12 at 22:55
  • The error is underlining the Dim command and it reads Specifiers valid only at the begining of a statement – Robert Apr 12 '12 at 22:55
  • @user512915 again my bad, out of practice with VB.Net right now. Fixed – JaredPar Apr 12 '12 at 22:57