Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Serializing an array

Posted on 2014-10-03
10
Medium Priority
?
123 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

877 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