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 7129137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:12:41+00:00 2026-05-28T11:12:41+00:00

public partial class Form1 : Form { string[] id; private void button_Click(object sender, EventArgs

  • 0
public partial class Form1 : Form
{
    string[] id;

private void button_Click(object sender, EventArgs e)
{
    char[] delimiters = { ',', '\r', '\n' };
    string[] content = File.ReadAllText(CSV_File).Split(delimiters);

    int x = content.GetUpperBounds(0)
    int z = 0;
    int i - 0;

    for (i = 0; i <= x / 3; i++)
        {
            z = (i * 3);
            id[i] = content[z]; // this line gives the error
        }

}
}

I want to get every 3rd value from array content, and put it into array id. This gives a ‘NullReferenceException was unhandled’ error and suggests I use ‘new’, but it is not a type or namespace. What should I do here?

They are both string arrays, and the error occurs on the first run so I do not think it is related to exceeding the bounds.

  • 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-28T11:12:41+00:00Added an answer on May 28, 2026 at 11:12 am

    This line of code:

    string[] id;
    

    is actually creating a null reference.

    When you declare an array, you have to explicitly create it, specifying the size.

    In your example, you have two alternatives

    1. Determine how big the array will be beforehand, and create the array length
    2. Actually populate a container that manages its own size.

    The first option:

    int x = content.GetUpperBounds(0)
    int z = 0;
    int i - 0;
    
    id = new string[x/3];
    for (i = 0; i <= x / 3; i++)
        {
            z = (i * 3);
            id[i] = content[x];
        }
    

    The second option:

    int x = content.GetUpperBounds(0)
    int z = 0;
    int i - 0;
    
    List<string> list = new List<string>();
    for (i = 0; i <= x / 3; i++)
        {
            z = (i * 3);
            list.Add(content[z]);
        }
    
    id = list.ToArray();
    

    The first option would perform better, as you are only allocating one object.

    Admittedly, I tend to disregard performance and use the second option, because it takes less brainpower to code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently, I have something like: public partial class Form1 : Form { delegate void
public partial class Form1 : Form { public disp(string strVal) { lbl1.text = strVal;
public partial class Form1 : Form { String Path1 = Application.StartupPath + \\component.xml; XmlDataDocument
Consider this source: public partial class FormTest : Form { private Test test {
I have this code: public partial class Form1 : Form { public Form1() {
public partial class Form1 : Form { MyClass myClass = new MyClass(one, two); public
public partial class Form1 : Form { bool AfterDocumentCompleted = false; int steps =
This is my MDI Parent. public partial class frmMain : Form { public Options
You should be able to create a generic form: public partial class MyGenericForm<T> :
Given this class public partial class Default : Page { private IRepository repo; ...

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.