Solved

Serializing an array

Posted on 2014-10-03
10
117 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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!

 

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
ASP.NET Calendar Control 5 54
VB.NET - Refactor Class per SOLID principles 2 43
Sending HTML Formatted Email from ASP.NET 2 57
Help with Report Viewer in VS2013 6 34
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

751 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