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

  • Home
  • SEARCH
  • 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 618847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:35:29+00:00 2026-05-13T18:35:29+00:00

I am trying to initialize a list of objects (which are of the type

  • 0

I am trying to initialize a list of objects (which are of the type Rep). Here is the code I am using to initialize the list:

        public static List<Rep> Reps = new List<Rep>(new Rep[6]);  

Right now when I try to assign a value to a string in one of the list’s classes like this:

 Repository.Reps[repnum].Main = new TextRange(richTextBox1.Document.ContentStart,
 richTextBox1.Document.ContentEnd).Text;

I a null reference exception. What am I doing wrong? I couldn’t find any Msdn documentation about setting the initial size of a list.

  • 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-13T18:35:29+00:00Added an answer on May 13, 2026 at 6:35 pm

    I think you might be getting your null reference exception because Repository.Reps[repnum] is null when you try to set the value of the Main property. What your doing is creating an Array of Reps with a size of 6, but all the references in that array you are giving to the List are null. Try newing up a Rep object and setting the Main property of it there like this:

    Rep newRep = new Rep();
    newRep.Main = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text;
    
    Repository.Reps[repnum] = newRep;
    

    If your intention is let all values in the List be null at first it might just be simpler to use the List(int) constructor and create the List in this way:

    public static List<Rep> Reps = new List<Rep>(6);
    

    However, if your intention is to have your List contain not null objects when you create it you can create the List in this way:

    public static List<Rep> Reps = new List<Rep>() 
    {
        new Rep(),
        new Rep(),
        new Rep(),
        new Rep(),
        new Rep(),
        new Rep()
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to initialize a vector which has integers 1 to n in
I'm a beginner at Objective-C and am trying to initialize an NSMutableArray with objects
I have a method void foo(list<shared_ptr<Base>>& myList); Which I'm trying to call with a
I am trying to initialize a reference object through the initialization list in the
I'm trying to display a list of projects using backbone.js. Basically, backbone should be
Trying to initialize a class called StackAsLinkedList, which should be a derived class of
I'm trying to implement a function that initializes an arbitrarily sized list with objects,
Trying to initialize and set things up for a class as follows: class Accel
I'm trying to initialize a Datatable with a default search value that the user
I'm trying to initialize a usercontrol that contains a gridview as hidden when a

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.