NullReferenceException:ObjectReference not set to an instance of an object

Hi,

I am using Vb.Net and XMl.My application works like this:I have to connect to a remote server(not on my network)thru a username and password given and check for login credentials. Once, the credentials are verified, I should be able to proceed.

When I compile the program, I get a warning that the fn SendXMLRequest doesn't return a value on all code paths. NullReference exception could occur at runtime.

I appreciate yr quick response and thanku in advance.

The 3 main functions I am using are:

Private Function SendXmlRequest(ByRef xml As String)
Try


'new
Dim xmpRequest As HttpWebRequest =
WebRequest.Create(XmpUrl)

'Feb14 Instantiate an XmlDocument object to avoid
NullReference exception

Dim xmlDoc As XmlDocument
xmlDoc = New XmlDocument
'System.Xml.XmlDocument(xmldoc = New
System.Xml.XmlDocument)


'Dim xmlDoc As XmlDocument = Nothing

'Dim xmlReader As XmlTextReader
'xmlReader = Nothing



xmpRequest.Method = "POST"

'Feb14 not to be modified

xmpRequest.KeepAlive = True
xmpRequest.ContentType = "text/xml"



Dim writer As StreamWriter
writer = New StreamWriter(xmpRequest.GetRequestStream())
writer.Write(xml)
writer.Close()





'Send the request and get the response
Dim HttpWResponse As HttpWebResponse =
CType(xmpRequest.GetResponse(), HttpWebResponse)

'Console.WriteLine("before sending status")

'Get the status and the headers

Dim iStatCode As Integer = HttpWResponse.StatusCode
Dim sStatus As String = iStatCode.ToString()

Console.WriteLine("Status: {0} {1}", sStatus,
HttpWResponse.StatusDescription.ToString())
Console.WriteLine("Request Headers:")
Console.WriteLine(xmpRequest)
Console.WriteLine("Response Headers:")
Console.WriteLine(HttpWResponse.Headers.ToString())


'feb15
If Not HttpWResponse Is Nothing Then
Dim XmlRead = New
XmlTextReader(HttpWResponse.GetResponseStream())
'Dim xmlDoc As XmlDocument
'xmlDoc = New XmlDocument
'Dim xmlDoc = New XmlDocument
xmlDoc.Load(XmlRead)
Return xmlDoc

'feb14

Else
Console.WriteLine("try again")

End If


'Get the response stream
Dim strm As Stream = HttpWResponse.GetResponseStream()




'Feb10
'Read the Response stream
Dim encode As Encoding =
System.Text.Encoding.GetEncoding("iso-8859-1")
Dim sr As New StreamReader(strm, encode)
Console.WriteLine("Response: {0}", sr.ReadToEnd())
sr.Close()
'new feb13
'Dim doc As New XmlDocument
'doc.Load(HttpWResponse.GetResponseStream())
'doc.Save(Console.Out)

'end
HttpWResponse.Close()



'Read the response stream
'Dim sr As StreamReader = New StreamReader(strm)
'Dim sText As String = sr.ReadToEnd()
'Console.WriteLine("Response: {0}", sText)

' Close the stream
'strm.Close()

'Clean up
'xmpRequest = Nothing
'HttpWResponse = Nothing
'MyCredentialCache = Nothing
'myCred = Nothing
'strm = Nothing
'sr = Nothing

'Feb14
'If sStatus = 200 Then
'The Http Request/Response was successful, now we need to
check the XML that was returned for errors
'If Not RequestHasErrrors(xmpRequest.responseRext) Then
'The overall request was successful so begin to parse the
response XML

'SendXmlRequest = xmpRequest.responseXml
'End If

'Else

'End If


'There was an Http error
'Console.WriteLine("Status Code: " & xmpRequest.status)
'Console.WriteLine("Status Code: " & iStatCode)
'Console.WriteLine("Status Text: " & sStatus)
'Console.WriteLine("Response Text: " &
HttpWResponse.StatusDescription.ToString)
'Console.WriteLine("Status Text: " & xmpRequest.statusText)
'Console.WriteLine("Response Text: " &
xmpRequest.responseText)
'End If
'Else
'Call MsgBox("There was an error. ReadyState = " &
xmpRequest.readyState)
'End If


Catch ex As NullReferenceException

MsgBox("Can't Request page" & vbCrLf & ex.Message)

End Try

End Function

Private Function RequestHasErrors(ByRef xml As String) As Boolean
' Check for the high level error Xml in any response before
' trying to parse the results.
If InStr(xml, "<Response><Error>") >= 1 Then
RequestHasErrors = True
End If
End Function

Private Function ParseLoginResponse(ByRef loginDom As XmlDocument)
As Boolean
' See if the login was successful. If so, get the SessionID.
' Otherwise, log the error.

Try


ParseLoginResponse = False


'Feb13
Dim filterDoc As New XmlDocument

'Feb16
'Instantiate an XmlNamespaceManager object
Dim nsmgr As XmlNamespaceManager
nsmgr = New XmlNamespaceManager(filterDoc.NameTable)


' Setup the Dom to use XPath queries and also setup
' an Xml namespace prefix for use in the XPath queries.

nsmgr.AddNamespace("ns", "urn:schemas -tms:LoginResponse")


Dim node As XmlNode

'Feb16
'node =
loginDom.SelectSingleNode("ns:LoginResponse/ns:Status")
node =
loginDom.SelectSingleNode("ns:LoginResponse/ns:Status", nsmgr)
'node = New XmlDocument

If node.Value = "Success" Then
' The login was 100% successful so get the SessionID.

'new Feb 14
'If node Is Nothing Then

'for testing
Console.WriteLine("Login successful: " & node.Value)
node =
loginDom.SelectSingleNode("ns:LoginResponse/ns:SessionID")



'new for testing Feb16
Dim resultImport As XmlNode
resultImport = filterDoc.ImportNode(node, True)
filterDoc.AppendChild(resultImport)
'Return True
g_SessionID = node.Value
ParseLoginResponse = True
Console.WriteLine("Login successful: " & node.Value)


'Feb14

Else
'The login was not 100% successful
node =
loginDom.SelectSingleNode("ns:LoginResponse/ns:SystemMessage")
Console.WriteLine("Login unsuccessful: " &
node.InnerText)



End If

Catch ex As Exception
MsgBox("Can't parse login information" & vbCrLf &
ex.Message)




loginDom = Nothing
'node = Nothing

End Try
End Function






Answer this question

NullReferenceException:ObjectReference not set to an instance of an object

  • John Simpson 2000

    Well, .... the compiler message says it all.

    Not all code paths return a value. This means that in some cases, your function will not return a value. And this is correct.

    A quick overview -I am really not going to go through those 200 orso lines of code- of your function learned that you only have one return statement, and that this statement is in the then branch of an if statement.

    This means that your function will only return a value, if the condition in that if statement is true.



  • NullReferenceException:ObjectReference not set to an instance of an object