Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3341472
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:43:35+00:00 2026-05-18T00:43:35+00:00

I am constructing a collection of objects, and need to use inheritance on a

  • 0

I am constructing a collection of objects, and need to use inheritance on a base class – I have been trying for hours in various ways and can’t get this to work whatsoever – the error is always “Object reference not set to an instance of an object.”

The idea in the example below is that an object is instantiated, and within that lies a collection of Students when I try to access the oDemo.Students(0).Name property the error is raised. Otherwise the object appears to have instantiated OK.

Here is the code being used to instantiate and access the class (from ASP.NET).

Dim oDemo As New MyDemo.Students
lblTest.Text = oDemo.Student(0).Name

Here is the sample code from the class library:

Public Class Students
    Private p_Students As System.Collections.Generic.List(Of Test)
    ReadOnly Property Student() As System.Collections.Generic.List(Of Test)
        Get
            Dim alTestNames As New ArrayList()
            alTestNames.Add("John")
            alTestNames.Add("Brian")
            alTestNames.Add("Scott")
            For iLoop As Integer = 0 To alTestNames.Count - 1
                p_Students.Add(New Test(alTestNames(iLoop).ToString))
            Next
            Return p_Students
        End Get
    End Property
End Class

Public MustInherit Class TestBase
    Private p_Name As String
    ReadOnly Property Name() As String
        Get
            Return p_Name
        End Get
    End Property
    Sub New(ByRef sName As String)
        p_Name = sName
    End Sub
End Class

Public Class Test
    Inherits TestBase
    Sub New(ByRef sName As String)
        MyBase.New(sName)
    End Sub
End Class

I’m not sure if I am totally missing the point in some way – any help is gratefully received. If any more info/background is needed please advise.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T00:43:36+00:00Added an answer on May 18, 2026 at 12:43 am

    You never initialized the p_Students field, it is Nothing and causes the exception. Fix:

    Private p_Students As New System.Collections.Generic.List(Of Test)
    

    Note the New keyword. That’s the quick fix, a more sane approach would be:

    Public Class Students
        Private p_Students As System.Collections.Generic.List(Of Test)
        Public Sub New()
            p_Students = New System.Collections.Generic.List(Of Test)
            p_Students.Add(New Test("John"))
            '' etc..
        End Sub
        ReadOnly Property Student() As System.Collections.Generic.IList(Of Test)
            Get
                Return p_Students
            End Get
        End Property
    End Class
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was recently profiling an application trying to work out why certain operations were
Using MVC2 Have a master-page that needs to hide certain menus if currently logged
I have an application that ships to the customer with a JET database including
I am looking for an efficient cache strategy for C#. I am constructing an
Let's say you have two collections of integers: IEnumerable<int> col1=new List<int> {2,3,3,5,7,11,11,11,13}; IEnumerable<int> col2=new
I am working with a large Entity Framework 4.0 edmx, and need to automate
I have a simple vbscript for retrieving the Windows version: Set objWMI = GetObject(winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2)
I'll ask this with a Scala example, but it may well be that this
I'm writing a linked list container for my homework. Using Qt 4.7 and gcc

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.