-1

I am trying to make this IF statement work but i am getting this error box. It says NullReferenceException was unhandled.

Here is my IF statement.

        If URL.Contains("www") = True Then
            objWriter.Write(codeLine23)
            objWriter.WriteLine()
            objWriter.Write(codeLine24)
            objWriter.WriteLine()
            objWriter.Write(URL)
            objWriter.WriteLine()

        Else

        End If

What am I doing wrong?

Thanks

Shaun

Shaun5
  • 57
  • 1
  • 8
  • 4
    See in particular the answer [dedicated to VB](http://stackoverflow.com/a/26761773/791010). Everyone hits this error at some point, the best answer is for you to not worry so much about this particular error right now, but instead go and learn how to use your debugging tools. (Given the code posted though, it appears that you've not instantiated `objWriter` and/or `URL`). – James Thorpe May 11 '16 at 16:05
  • Telling _exactly where_ you get it could help us. – Visual Vincent May 11 '16 at 16:13
  • @JamesThorpe Thank you for your advice. – Shaun5 Oct 16 '18 at 07:51

1 Answers1

-1

A NullReferenceException exception is thrown when you try to access a member on a type whose value is null. A NullReferenceException exception typically reflects developer error and is thrown when you've forgotten to instantiate a reference type.

In your example, in absence of indications about the line where the exception is thrown, it can potentially be anywhere were the code tries using any of the objWriter, codeLine32, codeLine24 or URL variables.

Assuming that objWriter was not instantiated, you would need to create an instance of objWriter before using it:

        If URL.Contains("www") = True Then
        If ()objWriter Is Nothing) Then
            objWriter  =  New System.IO.StreamWriter( FILE_NAME )
        endif
        objWriter.Write(codeLine23)

        objWriter.WriteLine()
        objWriter.Write(codeLine24)
        objWriter.WriteLine()
        objWriter.Write(URL)
        objWriter.WriteLine()
    Else
    End If
Jean-François
  • 374
  • 3
  • 8
  • To whom down voted my answer, it would be more constructive to indicate the reason of your vote. I agree that my answer is based on the assumption that the code fails due to the objWriter not being initialized. – Jean-François May 15 '16 at 18:08