-1

I have an ArrayList defined in Class A. Then I want to build this array in Class B and use it in Class A.

I defined the ArrayList as:

Public arrayList As ArrayList

Then, in Class B I do:

Dim trLogkEmpty As New A
'Loop with strEspece definition
    trLogkEmpty.arrayList.Add(strEspece)
'End Loop

The program throws me this error:

NullReferenceException

I don't know why, because strEspece has never become null (I tested it). I don't know if there is another reason.

Also, when I loop through the arrayList elements in Class A, I get again NullReferenceException. This is the loop code:

For Each logkNull In Me.arrayElemWithLogkEmpty
    Console.WriteLine(logkNull)
 Next

I don't know what happens with the first exception, but the code runs "correctly". In the second exception I guess that is something like I'm loosing the elements values of the array. I don't know how to solve it...any help? I accept different ways to solve it!

daro
  • 313
  • 1
  • 4
  • 17
  • Does your constructor for class A initialise `arrayList`? – Phylogenesis Jun 30 '15 at 15:35
  • In ClassA you need to create an instance of the ArrayList. For example `Public arrayList As New ArrayList` – Blackwood Jun 30 '15 at 15:36
  • 1
    No, the NRE is more likely because `trLogkEmpty` or `arrayList` is not initialized, not `strEspece` - we lack any context for this though with no real code. See also [NullReference Exception in Visual Basic](http://stackoverflow.com/a/26761773/1070452) for help on this. – Ňɏssa Pøngjǣrdenlarp Jun 30 '15 at 15:37

2 Answers2

1

You are making two of the same mistake. A NullReferenceException means that you are attempting to access a property or method on an object that hasn't been instantiated yet. You are attempting to access both A and A.arrayList without first creating new instances of them.

So, instead of just:

trLogkEmpty.arrayList.Add(strEspece)

You should have:

Dim trLogkEmpty As New A()
trLogkEmpty.arrayList = New ArrayList()
trLogkEmpty.arrayList.Add(strEspece)

However, I must insist that you avoid ArrayList, and also that you avoid instantiating a public member of a class from outside that class. I would suggest using a strongly-typed collection class such as List(Of T), and having a read-only property in A's take care of its instantiation and visibility so the collection (not its contents) can't be modified outside of A:

Public Class A

    Private _myList As IList(Of String)

    Public ReadOnly Property MyList As IList(Of String)
        Get
            If _myList Is Nothing Then
                _myList = New List(Of String)
            End If
            Return _myList
        End Get
    End Property

End Class

And now you have:

Dim trLogkEmpty As New A()
trLogkEmpty.MyList.Add(strEspece)

You're probably going to need to keep your instance of A around, so class B should probably look somewhat like:

Public Class B

    Private _a As A

    Public Sub New()
        _a = New A()
    End Sub

    ' ... your methods that use _a.MyList

End Class
Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
  • The OP states that an instance of ClassA has been created `Dim trLogkEmpty As New ClassA`. It does seem likely that no instance of the ArrayList has been created (since that is the only other possibility). – Blackwood Jun 30 '15 at 15:46
  • @Blackwood: Ah, yeah... missed that! Thanks – Cᴏʀʏ Jun 30 '15 at 15:52
0

I got it finally. When I initialised the array in class 'A' I forget to create an instance of ArrayList class, specifically, I forget to put New:

Public arrayElemWithLogkEmpty As New ArrayList

So, partly, @Blackwood was right!

Thank you all and forgive me for my basic knowledge about vb.net.

daro
  • 313
  • 1
  • 4
  • 17
  • I put the `Shared` keyword for the `arrayList` and declared `As New`, so I solved the problem with it. – daro Jul 01 '15 at 09:22
  • You made it a static member of class A, but are accessing it from class B. Is there any reason that it can't just be a member of Class B from the start? – Cᴏʀʏ Jul 01 '15 at 12:15
  • Yes, that is what I jus finally did. I declared the ArrayList as `Public Shared` in class B. Then I instantiate this variable in class A as: `B.arrayList.Count` For example. – daro Jul 01 '15 at 12:38