Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

object not set to instance of object when declaring a partial class

Posted on 2011-03-09
6
Medium Priority
?
367 Views
Last Modified: 2012-05-11
Hello experts,
I normally know how to fix this, and its usually because i forgot a new keyword somewhere.  That is not the case.  I am using a partial class that generates a serialized xml.  I create a new instance of the class but it is still telling me object not set to instance of an object.   I used the xsd tool to create the class from a provided schema.  The generated class is below, i took out a lot of unecessary variables.  I have never worked with partial classes so i am wondering if that is the cause.

In my code i call the below class like so:
Function create_receipt_xml(byval bod_id as string) As Boolean
        Dim as2 As New ShowReceiptInfo
        Dim serializer = New XmlSerializer(GetType(ShowReceiptInfo))

        Try
            as2.ApplicationArea.BODId = bod_id  'I get the object not set to instance of object here
            header_info ="test"
           Dim string_writer As New StringWriter()
            serializer.Serialize(string_writer, as2)
            Dim LogDir As String = System.Configuration.ConfigurationSettings.AppSettings("LogDirectory")
            Log_Files(LogDir, string_writer.ToString(), headerid)

            'Send_Transaction(string_writer.ToString(), HeaderId, strURL)
            string_writer.Close()
            string_writer.Dispose()
            as2 = Nothing
        Catch ex As Exception
            MessageBox.Show("Failed to create int100 xml! " & ex.Message)
        End Try
    End Function
'------------------------------------------------------------------------------
' <auto-generated>
'     This code was generated by a tool.
'     Runtime Version:2.0.50727.3615
'
'     Changes to this file may cause incorrect behavior and will be lost if
'     the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------

Option Strict Off
Option Explicit On

Imports System.Xml.Serialization

'
'This source code was auto-generated by xsd, Version=2.0.50727.42.
'

'''<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42"), _
 System.SerializableAttribute(), _
 System.Diagnostics.DebuggerStepThroughAttribute(), _
 System.ComponentModel.DesignerCategoryAttribute("code"), _
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:="http://mycompany.com/manufac"), _
 System.Xml.Serialization.XmlRootAttribute([Namespace]:="http://mycompany.com/ manufac ", IsNullable:=False)> _
Partial Public Class ShowReceiptInfo

    Private applicationAreaField As ShowReceiptInfoApplicationArea
    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
    Public Property ApplicationArea() As ShowReceiptInfoApplicationArea
        Get
            Return Me.applicationAreaField
        End Get
        Set(ByVal value As ShowReceiptInfoApplicationArea)
            Me.applicationAreaField = value
        End Set
    End Property

    End Property
End Class

'''<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42"),  _
 System.SerializableAttribute(),  _
 System.Diagnostics.DebuggerStepThroughAttribute(),  _
 System.ComponentModel.DesignerCategoryAttribute("code"),  _
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true, [Namespace]:="http://mycompany.com/manufac ")>  _
Partial Public Class ShowReceiptInfoApplicationArea
    
    Private bODIdField As String
    
<System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>  _
    Public Property BODId() As String
        Get
            Return Me.bODIdField
        End Get
        Set
            Me.bODIdField = value
        End Set
    End Property
    
End Class

Open in new window

0
Comment
Question by:tentavarious
[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
  • 4
  • 2
6 Comments
 
LVL 4

Expert Comment

by:parnasso
ID: 35085592
I think the compiler is telling you that the variable applicationAreaField has not been initialized. When declaring a class partial, the default constructor for your ShowReceiptInfo class is not doing something like : Dim applicationAreaField As New ShowReceiptInfoApplicationArea.

Declare another Partial Public Class ShowReceiptInfo with a default constructor that initializes the field applicationAreaField.

I hope my explanation was clear

0
 

Author Comment

by:tentavarious
ID: 35085636
Here is the xml schema i used to create class above

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
           targetNamespace="http://mycompany.com/manufac "
           xmlns:pre="http://mycompany.com/manufac">
  <xs:element name="ShowReceiptInfo">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="ApplicationArea" form="unqualified">
                  <xs:element name="BODId" form="unqualified" type="xs:string"/>
              </xs:sequence>
          </xs:complexType>
        </xs:element>
       <xs:attribute name="environment" use="required"/>
      <xs:attribute name="lang" use="required"/>
      <xs:attribute name="revision" use="required"/>
    </xs:complexType>
  </xs:element>
</xs:schema>
0
 

Author Comment

by:tentavarious
ID: 35085644
Could you provide an example, the code compiles fine, i get the error when i try and run it
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 4

Accepted Solution

by:
parnasso earned 2000 total points
ID: 35085751
I didn't explained myself clearly. In addition to the code which is generated automatically, you can declare in your manually written code another part of the partial class:

Partial Public Class ShowReceiptInfo

    Private applicationAreaField As ShowReceiptInfoApplicationArea

    Private Sub Constructor()
        applicationAreaField =  New ShowReceiptInfoApplicationArea; // or default value you want
    End Sub

Open in new window


Then when you do in your code:

 as2.ApplicationArea.BODId = bod_id

It will work because the ApplicationArea now is not null.

By the way I'm sorry about the syntax errors in my VB.

0
 

Author Comment

by:tentavarious
ID: 35085883
Still no go
I tried adding the constructor and  doing this

Private applicationAreaField As New ShowReceiptInfoApplicationArea
0
 

Author Comment

by:tentavarious
ID: 35086160
Alright it worked thanks a lot, maybe you could take a look at another xml issue i am having.  It deals with the same project,  for some reason when i create the xml any tag i have containing a ":" gets replaced

take a look at this thread

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_26870958.html
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

636 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