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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:25:11+00:00 2026-05-31T20:25:11+00:00

I am getting the object reference error in this line: emp[count].emp_id = int.Parse(parts[0]); in

  • 0

I am getting the object reference error in this line:
emp[count].emp_id = int.Parse(parts[0]);

in this code

this program to read from file and store in array of object

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public class employees
    {
        public int emp_id;
        public string firstName;
        public string lastName;
        public double balance;
    }

    private void btnOpen_Click(object sender, EventArgs e)
    {
        OpenFileDialog file = new OpenFileDialog();
        DialogResult result = file.ShowDialog();
        if (result == DialogResult.Cancel) return;

        string fileName = file.FileName;
        StreamReader reader = new StreamReader(fileName);

        string[] lines = File.ReadAllLines(fileName);
        int emp_count = lines.Count<string>();
        employees[] emp = new employees[emp_count];
        int count = 0;
        foreach (string line in lines)
        {
            string[] parts = new string[4];
            parts = line.Split(',');
            **emp[count].emp_id = int.Parse(parts[0]);**
            emp[count].firstName = parts[1];
            emp[count].lastName = parts[2];
            emp[count].balance = double.Parse(parts[3]);
            count++;
            txtGet.Text += emp[count].emp_id + " " + emp[count].firstName + " " + emp[count].lastName + " " + emp[count].balance + " \n ";

        }
  • 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-31T20:25:12+00:00Added an answer on May 31, 2026 at 8:25 pm

    You need to initialise emp[count] to something.

    You can do this by adding the following:

    foreach (string line in lines) 
    { 
        emp[count] = new employees();
        string[] parts = new string[4]; 
        //....
    }
    

    When you call employees[] emp = new employees[emp_count]; you initilise emp to an array of employees with the length of emp_count.

    emp[0] = null;
    emp[1] = null;
    //etc.
    

    Each element inside emp also needs to be instantiated before you can use it.

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

Sidebar

Related Questions

I keep getting this error when I run the program. Object reference not set
This script is getting Microsoft JScript runtime error: Object expected on If (out_params.bGranted) line.
I'm getting the error Object reference not set to an instance of an object.
I'm getting the NRE error that says: Object reference not set to an instance
I have been getting an error in VB .Net object reference not set to
I'm getting the following error in my C++ program: 1> c:\users\thom\documents\cworkspace\barnaby\barnaby\timezone.cpp(14) : see reference
I am getting error saying: Object reference not set to an instance of an
Object reference not set to an instance of an object. i am getting this
I don't undertand why I'm getting this error, code was working and running perfectly
Why am I getting an error for this piece of code?: function catchevent() {

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.