Click here to Skip to main content
16,019,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have been trying to link my "person" class values with "workers" class in the "Read" method.
When I write
Console.WriteLine(person.GetName());
it gives me all the values perfectly, but if I write
Console.WriteLine(workers.Get(i).GetName());
it shows "Object reference not set to an instance of an object." I have written "workers.Set(person);" and I thought that this would link the "person" with "workers", but as I can see, it doesn't, and I dont know what I did wrong and how to fix it. How do I properly link "workers" with "person"?
My code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace L5
{
    class Program
    {
        class Person
        {
            private string surname;
            private string name;
            private string bank;
            private string num;
            private int pieces;

            public Person()
            {
                surname = "";
                name = "";
                bank = "";
                num = "";
                pieces = 0;
            }
            public void Set(string surname, string name, string bank, string num)
            {
                this.surname = surname;
                this.name = name;
                this.bank = bank;
                this.num = num;
            }
            public void SetPieces(int piece) { pieces = piece; }
            public string GetName() { return name; }
            public string GetSurname() { return surname; }
            public string GetBank() { return bank; }
            public string GetNum() { return num; }
            public int GetPieces() { return pieces; }

            public override string ToString()
            {
                string s;
                s = string.Format("{0, -15} {1, -10} {2,-10} {3, -30}",
                surname, name, bank, num);
                return s;
            }
        }
        class Matrix
        {
            const int CMaxEil = 10;
            const int CMaxSt = 100;
            private Person[] A;
            private int[,] W;
            public int n { get; set; }
            public int m { get; set; }

            public Matrix()
            {
                n = 0;
                A = new Person[CMaxSt];

                m = 0;
                W = new int[CMaxEil, CMaxSt];
            }

            public Person Get(int nr) { return A[nr]; }

            public void Set(Person ob) { A[n++] = ob; }

            public void SetW(int i, int j, int r) { W[i, j] = r; }

            public int GetW(int i, int j) { return W[i, j]; }
        }
        static void Main(string[] args)
        {
            const string Cfd = "..\\..\\data.txt";
            const string Cfr = "..\\..\\results.txt";
            Matrix workers = new Matrix();
            Read(Cfd, ref workers);
            if (File.Exists(Cfr))
                File.Delete(Cfr);
        }
        static void Read(string fd, ref Matrix workers)
        {
            int nn, mm, number;
            string surname, name, bank, num;
            double price;
            string line;
            Person person;
            using (StreamReader reader = new StreamReader(fd))
            {
                line = reader.ReadLine();
                string[] parts = line.Split(' ');
                nn = int.Parse(parts[0]);
                mm = int.Parse(parts[1]);
                workers.n = nn;
                workers.m = mm;
                price = double.Parse(parts[2]);
                for (int i = 0; i < nn; i++)
                {
                    line = reader.ReadLine();
                    string[] partss = line.Split(' ');
                    surname = partss[0];
                    name = partss[1];
                    bank = partss[2];
                    num = partss[3];
                    person = new Person();
                    person.Set(surname, name, bank, num);
                    workers.Set(person);
                   // Console.WriteLine(person.GetName());
                   // Console.WriteLine(workers.Get(i).GetName());
                }
                for (int i = 0; i < nn; i++)
                {
                    line = reader.ReadLine();
                    string[] partsss = line.Split(' ');
                    for (int j = 0; j < workers.m; j++)
                    {
                        number = int.Parse(partsss[j]);
                        workers.SetW(i, j, number);
                    }
                }
            }
        }
    }
}


And my data that I read (just in case):
4 7 2.99
Surname Name Seb 988957532897855
Surnamee Namee Swedbank 623378348393499
Surnameee Nameee Luminor 234132535698906
Surnameeee Nameeee Seb 989677678986896
5 7 2 9 9 3 5
1 2 3 1 4 1 1
8 9 8 7 6 9 9
4 6 7 4 3 8 3


What I have tried:

-----------------------------------------------------
Posted
Updated 8-Dec-20 10:07am
Comments
BabyYoda 8-Dec-20 15:28pm    
If you debug it you'll find that at some value for i you do not have the object. You just need to fix that.

1 solution

That is why normally it is recommended to not have a long chain of method calls, if any single one of them fails you get a null reference exception.

I would suggest rewriting that log line as:
C#
var worker = workers.Get(i);
if(worker != null) {
    Console.WriteLine(worker.GetName());
}

Secondly, there is also a safe-navigation operator that you can use which only propagates the call if the object is not null.

See these for more on that:
Member access operators and expressions - C# reference | Microsoft Docs[^]
 
Share this answer
 
Comments
BillWoodruff 8-Dec-20 16:41pm    
+5
Afzaal Ahmad Zeeshan 8-Dec-20 19:36pm    
Thank you, Bill!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900