Converting Class object to Json for WebService

alwaysVBNET

I am trying to return an object of my class Product as Json for a webservice. The object is loaded with all the values but I get an error when it attempts to return it as Json. Here is my method:

 <AllowAnonymous> <HttpGet> _
    Public Function GetProduct() As HttpResponseMessage
        Try
            Dim oProduct As New Product
            oProduct = Product.GetProductByID(25)
            Dim jsSerializer As System.Web.Script.Serialization.JavaScriptSerializer
            jsSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()
            Dim sbProduct As New System.Text.StringBuilder()
            jsSerializer.Serialize(oProduct, sbProduct)
            Dim json As String = sbProduct.ToString()
            Return Request.CreateResponse(HttpStatusCode.OK, json)
        Catch exc As Exception
            Return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exc)
        End Try
    End Function

The response:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
Exception has been thrown by the target of an invocation.
</ExceptionMessage>
<ExceptionType>System.Reflection.TargetInvocationException</ExceptionType>
<StackTrace>
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Web.SecurityUtils.MethodInfoInvoke(MethodInfo method, Object target, Object[] args) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember) at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output) at Christoc.Modules.top3all.ModuleTaskController.GetProduct() in C:\inetpub\wwwroot\top3\DesktopModules\top3all\Models\WebServices.vb:line 30
</StackTrace>
<InnerException>
<Message>An error has occurred.</Message>
<ExceptionMessage>
Object reference not set to an instance of an object.
</ExceptionMessage>
<ExceptionType>System.NullReferenceException</ExceptionType>
<StackTrace>
at DotNetNuke.Security.Permissions.PermissionProvider.HasModuleAccess(SecurityAccessLevel accessLevel, String permissionKey, ModuleInfo moduleConfiguration) at DotNetNuke.Security.Permissions.ModulePermissionController.HasModuleAccess(SecurityAccessLevel accessLevel, String permissionKey, ModuleInfo moduleConfiguration) at DotNetNuke.UI.Modules.ModuleInstanceContext.LoadActions(HttpRequest request) at DotNetNuke.UI.Modules.ModuleInstanceContext.get_Actions() at DotNetNuke.Entities.Modules.PortalModuleBase.get_Actions()
</StackTrace>
</InnerException>
</Error>

Edit #1 Definition of Product

Public Class Product Inherits AddNew

Public Shared selectedProduct As New Product

Private iProductID As Integer
Public Property ProductID() As Integer
    Get
        Return iProductID
    End Get
    Set(ByVal value As Integer)
        iProductID = value
    End Set
End Property

Private iCategoryID As Integer
Public Property CategoryID() As Integer
    Get
        Return iCategoryID
    End Get
    Set(ByVal value As Integer)
        If IsNothing(value) Or IsDBNull(value) Then
            iCategoryID = 0
        Else
            iCategoryID = value
        End If
    End Set
End Property

Private iSubCategoryID As Integer
Public Property SubCategoryID() As Integer
    Get
        Return iSubCategoryID
    End Get
    Set(ByVal value As Integer)
        If IsNothing(value) Or IsDBNull(value) Then
            iSubCategoryID = 0
        Else
            iSubCategoryID = value
        End If
    End Set
End Property

Private sName As String
Public Property Name() As String
    Get
        Return sName
    End Get
    Set(ByVal value As String)
        sName = value
    End Set
End Property

Private sDescription As String
Public Property Description() As String
    Get
        Return sDescription
    End Get
    Set(ByVal value As String)
        sDescription = value
    End Set
End Property

Private dPrice As Double
Public Property Price() As Double
    Get
        Return dPrice
    End Get
    Set(ByVal value As Double)
        If IsNothing(value) Or IsDBNull(value) Then
            dPrice = 0.0
        Else
            dPrice = value
        End If
    End Set
End Property

Private sCurrencyCulture As String
Public Property CurrencyCulture() As String
    Get
        Return sCurrencyCulture
    End Get
    Set(ByVal value As String)
        sCurrencyCulture = value
    End Set
End Property

Private sKeywords As String
Public Property Keywords() As String
    Get
        Return sKeywords
    End Get
    Set(ByVal value As String)
        sKeywords = value
    End Set
End Property

Private sCountry As String
Public Property Country() As String
    Get
        Return sCountry
    End Get
    Set(ByVal value As String)
        sCountry = value
    End Set
End Property

Private iViews As Integer
Public Property Views() As Integer
    Get
        Return iViews
    End Get
    Set(ByVal value As Integer)
        If IsNothing(value) Or IsDBNull(value) Then
            iViews = 0
        Else
            iViews = value
        End If
    End Set
End Property

Private iRating As Integer
Public Property Rating() As Integer
    Get
        Return iRating
    End Get
    Set(ByVal value As Integer)
        If IsNothing(value) Or IsDBNull(value) Then
            iRating = 0.0
        Else
            iRating = value
        End If

    End Set
End Property

Private dDateAdded As DateTime
Public Property DateAdded() As DateTime
    Get
        Return dDateAdded
    End Get
    Set(ByVal value As DateTime)
        dDateAdded = value
    End Set
End Property

Private sAffiliateLink As String
Public Property AffiliateLink() As String
    Get
        Return sAffiliateLink
    End Get
    Set(ByVal value As String)
        sAffiliateLink = value
    End Set
End Property

Private sAffiliateID As String
Public Property AffiliateID() As String
    Get
        Return sAffiliateID
    End Get
    Set(ByVal value As String)
        sAffiliateID = value
    End Set
End Property

Private iAddedBy As Integer
Public Property AddedBy() As Integer
    Get
        Return iAddedBy
    End Get
    Set(ByVal value As Integer)
        iAddedBy = value
    End Set
End Property

Private iImage As Integer
Public Property Image() As Integer
    Get
        Return iImage
    End Get
    Set(ByVal value As Integer)
        iImage = value
    End Set
End Property

Private iThumbnail As Integer
Public Property Thumbnail() As Integer
    Get
        Return iThumbnail
    End Get
    Set(ByVal value As Integer)
        iThumbnail = value
    End Set
End Property

Private iModifiedBy As Integer
Public Property ModifiedBy() As Integer
    Get
        Return iModifiedBy
    End Get
    Set(ByVal value As Integer)
        iModifiedBy = value
    End Set
End Property


Private dtModifiedTimeUtc As DateTime
Public Property ModifiedTimeUtc() As DateTime
    Get
        Return dtModifiedTimeUtc
    End Get
    Set(ByVal value As DateTime)
        dtModifiedTimeUtc = value
    End Set
End Property

Private sSource As String
Public Property Source() As String
    Get
        Return sSource
    End Get
    Set(ByVal value As String)
        sSource = value
    End Set
End Property

Private iModuleID As Integer
Public Property ModID() As Integer
    Get
        Return iModuleID
    End Get
    Set(ByVal value As Integer)
        iModuleID = value
    End Set
End Property

Private iPortalID As Integer
Public Property PortID() As Integer
    Get
        Return iPortalID
    End Get
    Set(ByVal value As Integer)
        iPortalID = value
    End Set
End Property

Any ideas how to fix this?

Andrew Van Til

Some member in Product or in the inheritance chain is probably uninitialized. This can be easy to miss, especially in VB.NET.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related