Serializing an array

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.
macypAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
What does the definition of LoadFromXMLData look like?
0
macypAuthor Commented:
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
Jacques Bourgeois (James Burger)PresidentCommented:
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
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

macypAuthor Commented:
But I am initializing the Status in the below code:
  Public Sub New()
        Me.statusField = New MedfusionService.MessageStatus
    End Sub
0
macypAuthor Commented:
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
käµfm³d 👽Commented:
Can you show the XML being deserialized? The data doesn't need to be real, but the structure should be accurate.
0
macypAuthor Commented:
<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
Jacques Bourgeois (James Burger)PresidentCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
käµfm³d 👽Commented:
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
macypAuthor Commented:
The XMLData length was 0, hence the error.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.