Solved

Serializing an array

Posted on 2014-10-03
10
111 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 74

Expert Comment

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

Author Comment

by:macyp
Comment Utility
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

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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
 

Author Comment

by:macyp
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
<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
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
The XMLData length was 0, hence the error.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now