Solved

Serializing an array

Posted on 2014-10-03
10
116 Views
Last Modified: 2014-10-03
I am trying to serialize a class in VB.NET. and I get 'Object reference not set to an instance of an object' error. I am having problems trying to make it work because it is an array. I do not know the size of the array up front. Here is the code below:

VB class (that is produced from a xsd)
-------------------------------------------------------------
Public Class ProcessingStatus

    Private messageField() As ProcessingStatusMessage

   Public Property Message() As ProcessingStatusMessage()
        Get
            Return Me.messageField
        End Get
        Set(value As ProcessingStatusMessage())
            Me.messageField = Value
        End Set
    End Property
End Class
------------------------------------------------------------------------------------------------------------------------------------------------------
Public Class ProcessingStatusMessage
  Private statusField As MessageStatus
 
    Public Sub New()
        Me.statusField = New MedfusionService.MessageStatus
    End Sub
End Class
-------------------------------------------------------------------------------------------------------------------------------------------------------

Code from where these classes are called:

 Dim ps As New ProcessingStatus
  ps = XMLHandler.LoadFromXMLData(ps.GetType, sr.ReadToEnd)

 Dim psm As New List(Of MedfusionService.ProcessingStatusMessage)

                            For Each this_psm As MedfusionService.ProcessingStatusMessage In ps.Message
                                psm.Add(this_psm)
                                Try
                                    If this_psm.Status.State <> "ERROR" Then
                                       '' Do Something
                                    Else
                                Catch ex As Exception
                                    Log.Error(ex)
                                End Try
  Next this_psm

I am getting the 'Object reference not set to an instance of an object' error in the For Each line. If the question is not clear, I can attach the full code.
0
Comment
Question by:macyp
  • 5
  • 3
  • 2
10 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40359581
What does the definition of LoadFromXMLData look like?
0
 

Author Comment

by:macyp
ID: 40359592
Public Shared Function LoadFromXMLData(ByVal ObjType As Type, ByVal XMLData As String) As Object

        Dim xs As New System.Xml.Serialization.XmlSerializer(ObjType)
        Dim sr As New System.IO.StringReader(XMLData)

        Dim obj As Object
        Try
            obj = xs.Deserialize(sr)
            sr.Close()

        Catch ex As Exception
            Log.Debug(ex)
        End Try
        Return obj

    End Function
0
 
LVL 40
ID: 40359677
The problem is probably that Status was not initialized before serialization. So, when you deserialize, it is still nothing and you cannot use it for a comparison.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:macyp
ID: 40359690
But I am initializing the Status in the below code:
  Public Sub New()
        Me.statusField = New MedfusionService.MessageStatus
    End Sub
0
 

Author Comment

by:macyp
ID: 40359708
Just to test if it has anything to do with Status, I commented the lines that compare the Status to error, but I still get the same error 'Object reference' on the For Each line.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40359717
Can you show the XML being deserialized? The data doesn't need to be real, but the structure should be accurate.
0
 

Author Comment

by:macyp
ID: 40359724
<ns2:ProcessingStatus>
    <DataJobId>1234</DataJobId>
    <Message>
        <MessageId>123</MessageId>
        <PracticeResourceId>5</PracticeResourceId>
        <Status>
            <State>COMPLETED</State>
            <StatusDate>2014-10-02T20:08:58</StatusDate>
        </Status>
    </Message>
</ns2:ProcessingStatus>
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 40359747
Sorry, I did not read your question carefully. I was in a rush, saw ERROR and assumed that this was the line that caused the error. Somehow my mind saw a ' where there was none.

Looking at it more carefully, I would rather move my detective work to LoadFromXMLData.

What is the length of XMLData. If it is zero, then there is nothing to deserialize.

If obj is Nothing, then Nothing is returned and ps is Nothing in your For Each instruction.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40359775
Is this the full XML that you are trying to deserialize? It certainly shouldn't be, because you've got a namespace prefix in use that isn't (visibly) defined anywhere.
0
 

Author Closing Comment

by:macyp
ID: 40359860
The XMLData length was 0, hence the error.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question