MyXmlDoc.LoadXml(File) Returns NilObjectException?

Here is my code:

Sub Decode()
  Dim OpenXmlDialog As New OpenDialog
  
  OpenXmlDialog.Title = "Select A Riivolution XML File"
  OpenXmlDialog.Filter = RiivolutionFileType.RiivolutionXMLFile
  OpenXmlDialog.InitialDirectory = SpecialFolder.Documents
  
  TheFile = OpenXmlDialog.ShowModal
  
  If TheFile <> Nil Then
    Try
      TheRiivolutionXmlDocument.LoadXml(TheFile) // <-- This is the NilObjectException.
    Catch error As XmlException
      MsgBox "XML Error: " + error.Message
    End Try
    
    If TheRiivolutionXmlDocument.DocumentElement.Name = "wiidisc" = False Then // <-- Can I do this? (if text = value = true)
      MsgBox "Please supply a XML file that was created for Riivolution!"
    Else
      
    End If
  End If
  
End Sub

tl;dr:

  1. I get a NilObjectException at TheRiivolutionXmlDocument.LoadXml(TheFile). I tried putting a path variable like “NativePath” and no go.
  2. Can I use the If statement like this: “If Thing.Text = “Hello” = False Then … End If” If not, then how can I use “If Not” or something similar?

Thanks.

It sounds like TheRiivolutionXmlDocument is nil.

You must instantiate TheRiivolutionXmlDocument before you can use it. LoadXML doesn’t create an XMLdocument, it just populates an existing object.

I hate these types of answers that just repeat the error message. Of course it’s nil, I’m receiving a NilObjectException.

Thanks for an actual, helpful, answer :slight_smile:

Well, I thought you were confused as to where the NilObjectException was coming from, thinking it was generated by TheFile. It also didn’t occur to me that that you didn’t know that you had to use “new” to create instances of objects, so that’s my bad.

On the other hand, I was trying to help and just got insulted for my trouble, so you won’t have to worry about any of my “unhelpful” answers in the future.

An if statement by itself tests if an expression is true or false, so the “= false” shouldn’t work as far as I know. Instead you should use:

if NOT thing.text="Hello" then
//thing is not "Hello" so execute code
end if

You could also use “<>,” which basically means “anything other than,” instead of the “not”.

“= false” does work. The first comparison yields a boolean, which is then compared to false–a comparison which always yields the same result as the Not operator. (That said, using <> is probably better than either, as it simply makes one comparison instead of two comparisons or a comparison and a reversal.)

You’re right, of course, but there are times when you might need to write it that way, perhaps for clarity. In those cases, use parens to make it clear:

if ( this = that ) = false then … // I'd never do this myself, but...

Or, more likely:

dim status as boolean = ( this = that )

I should stress that the parens are not required there, just, IMO, easier to read.