-1

Hi I have the following code

Partial Public Class DT_AnalyseDetail

    Private componentField As String
    Private contentField As String

    Public Property Component() As String
    Get
        Return Me.componentField
    End Get
    Set(value As String)
        Me.componentField = Value
    End Set
    End Property

    Public Property Content() As String
    Get
         Return Me.contentField
    End Get
    Set(value As String)
        Me.contentField = Value
    End Set
    End Property
End Class

Partial Public Class DT_Analyse

    Private detailField() As DT_AnalyseDetail
    Private batchField As String

    Public Property Detail() As DT_AnalyseDetail()
    Get
        Return Me.detailField
    End Get
    Set(value As DT_AnalyseDetail())
        Me.detailField = value
    End Set
    End Property

    Public Property Batch() As String
    Get
        Return Me.batchField
    End Get
    Set(value As String)
        Me.batchNoField = Value
    End Set
    End Property
End Class

Sub()

    Dim a_Myana(1) As DT_Analyse
    Dim a_Mydet(1) As DT_AnalyseDetail

    a_Mydet(0) = New DT_AnalyseDetail
    a_Mydet(0).Component = "TEST"
    a_Mydet(0).Content= "YES"

    a_Myana(0) = New DT_Analyse
    a_Myana(0).Batch= "123"
    a_Myana(0).Detail(0) = New DT_AnalyseDetail
    a_Myana(0).Detail(0) = a_Mydet(0)

End Sub

The Error happens when I want to fill a_Myana(0).Detail(0) = a_Mydet(0) I guess that my Class / array defintions are not correct. But I am not sure how to handle it. Does "Detail" need to have more dimensions? The class defintion was written by visual studios xsd.exe from an xesd file.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
user49017
  • 25
  • 9
  • 1
    Duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Bjørn-Roger Kringsjå Jul 02 '15 at 16:03
  • 1
    Nearly all NullReference Exceptions have the same set of causes. See [NullReference Exception in Visual Basic](http://stackoverflow.com/a/26761773/1070452) for help on this. `Public Sub()` just all alone there is invalid code – Ňɏssa Pøngjǣrdenlarp Jul 02 '15 at 16:04
  • That is true, I saw that before, but it does not help me finding my error in my excample. The Public Sub was just put into it to make clear the defnition of the class is done "outside". (Ill check Plutonix link with the arrays tomorrow-thanks) – user49017 Jul 02 '15 at 16:05
  • In that case, the answer is absolutely covered in the linked dupe (see **Array of class objects**). (lone `Sub` is still non compiling code - it needs a name) – Ňɏssa Pøngjǣrdenlarp Jul 02 '15 at 16:08
  • 1
    `a_Mydet(1)` is null aka. Nothing. `Nothing` do not have a property named `Component`. You need to instantiate a `DT_AnalyseDetail` object before setting the `Component` property: `a_Mydet(1) = New DT_AnalyseDetail()` – Bjørn-Roger Kringsjå Jul 02 '15 at 16:09
  • And if you don't know, arrays in .net are zero-based. The first item in the array is `a_Mydet(0)`. And `Dim a_Mydet(1)` instantiate an array of size `2`. – Bjørn-Roger Kringsjå Jul 02 '15 at 16:12
  • 1
    Perfect, thank you, such a small mistake. I know the zero based, in my real code is done via an integer, so that is ok, thank you. – user49017 Jul 02 '15 at 16:14

2 Answers2

2

This line does not do everything you think it does:

Dim a_Mydet(1) As DT_AnalyseDetail

You believe it creates an array of DT_AnalyseDetail objects, but this does not happen. Instead, it creates an array of DT_AnalyseDetail object references. At this point, each of those references has a value of Nothing (the references don't refer to any object). These Nothing references do not have space allocated for the Component property yet, so the line with the assignment fails.

BTW, there are two references here, not one, as VB.Net arrays are 0-indexed by default.

To fix the code, you need to do this:

Sub()

    Dim a_Mydet(1) As DT_AnalyseDetail
    a_Mydet(1) = New DT_AnalyseDetail()
    a_Mydet(1).Component = "TEST"

End Sub

.


Looking now at the updated question.

You have these lines:

a_Myana(0) = New DT_Analyse
a_Myana(0).Batch= "123"
a_Myana(0).Detail(0) = New DT_AnalyseDetail
a_Myana(0).Detail(0) = a_Mydet(0)

Here, you are expecting the New DT_Analyse expression to create a new array for the Detail property. This does not happen. Arrays are objects. Properties are object references. Just like with the previous issue, you must first make sure the object reference refers to something other than Nothing. You need to do this:

a_Myana(0) = New DT_Analyse
a_Myana(0).Batch= "123"
 a_Myana(0).Detail = New DT_AnDT_AnalyseDetail(0)
a_Myana(0).Detail(0) = New DT_AnalyseDetail
a_Myana(0).Detail(0) = a_Mydet(0)
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Yeah I got that. Thank you. But a new problem came up when I use this in the fashion that I have an array within an array. I will add it the problem in the question. – user49017 Jul 03 '15 at 08:30
0

Instead of

a_Myana(0).Detail(0) = New DT_AnalyseDetail
a_Myana(0).Detail(0) = a_Mydet(0)

I set

a_Myana(0).Detail() = a_Mydet

and it works :)

thomasb
  • 5,816
  • 10
  • 57
  • 92
user49017
  • 25
  • 9