0

I apologize for such a simple question, but I've been working on fixing this code for several hours now and I can't make any headway.

I have a project that calls for the usage of three classes: a customer, a employee, and a customer group. The classes are populated from file (.csv) and are then dumped back onto console displaying their contents. The customer group class which contains both a single instance of the employee class and a list of customer(s) as well as a couple of other lists that handle a couple of class unique variables. When dumping out my code I get some Unhandled Exception errors, and debugging has shown that the customer class within the customer group class is null. For all I know, there could be an entire range of other issues, but getting this list of a class populated is the problem.

The code itself is pretty long, so I'm going to try to trim it down for brevity's sake.

The Program bit in question:

while (dataArray[i] == "End")
        {
            int.TryParse(dataArray[i], out temp);
            i++;
            testGrp.Customers.Add(new Customer(temp, dataArray[i++], dataArray[i++], dataArray[i++], dataArray[i++]));
            double.TryParse(dataArray[i], out doubleTemp);
            testGrp.AmountSpent.Add(doubleTemp);
            i++;
            testGrp.SpendingLevel.Add(dataArray[i]);
            i++;
        }

The Customer Group Class:

public CustomerGrp(int groupId, Employee executive, List<Customer> customers,   List<double> amountSpent, List<string> spendingLevel)
    {
        this.groupId = groupId;
        this.executive = executive;
        this.customers = customers;
        this.amountSpent = amountSpent;
        this.spendingLevel = spendingLevel;
    }

And the customer class:

public Customer(int newId, string newName, string newPhoneNumber, string newAddress, string newMarried)
    {
        this.Id = newId;
        this.Name = newName;
        this.PhoneNumber = newPhoneNumber;
        this.Address = newAddress;
        this.Married = newMarried;
    }

dataArray is an array generated from breaking the initial string from the csv file into smaller bits. It's not pretty, but it serves its purpose for now. Prior to this, the groupID and the executive bits have been handled, ending in a i++ to prep for the part shown.

I can get the Employee executive part populated without errors, but populating a list of classes within a class is something I can't quite get my head around. I think I'm doing it right, but most examples I can find don't quite fit this situation. I know my code is anything but pretty, but I'm just trying to establish basic functionality before I start cleaning up. Any help or suggestions would be appreciated.

EDIT

As asked, the message given in console is as follows:

System.NullReferenceException Object Reference Not Set To an instance of the object. at 'line' and 'line'.

The lines are:

DumpContents(testGrp);

and

static void DumpContents(CustomerGrp customerGrp)
    {
        Console.WriteLine("------- Customer Content -------");
        Console.WriteLine("         Id: {0}", customerGrp.GroupId);
        DumpContents(customerGrp.Executive);
        foreach (Customer cust in customerGrp.Customers)
        {
            DumpContents(cust); // <- Exception Error line here
        }
        Console.WriteLine("--------------------------------");
    }

EDIT

Included the overloaded DumpContents function:

static void DumpContents(Employee employee)
     {
        Console.WriteLine("------- Employee Content -------");
        Console.WriteLine("         Id: {0}", employee.Id);
        Console.WriteLine("       Name: {0}", employee.Name);
        Console.WriteLine(" Start Date: {0}", employee.StartDate);
        Console.WriteLine("       Rate: {0}", employee.GetRate());
        Console.WriteLine("      Hours: {0}", employee.GetHours());
        Console.WriteLine("        Pay: {0}", employee.CalcPay());
        Console.WriteLine("     Tenure: {0} Years", employee.GetTenure());
        Console.WriteLine("--------------------------------");
    }

    static void DumpContents(Customer customer)
    {
        Console.WriteLine("------- Customer Content -------");
        Console.WriteLine("            Id: {0}", customer.Id);
        Console.WriteLine("          Name: {0}", customer.Name);
        Console.WriteLine("  Phone Number: {0}", customer.PhoneNumber);
        Console.WriteLine("       Address: {0}", customer.Address);
        Console.WriteLine("Marital Status: {0}", customer.Married);
        Console.WriteLine("--------------------------------");
    }
  • Welcome to [so], could you post the `Unhandled Exception errors`? and when you say * but populating a list of classes within a class is something I can't quite get my head around* - can you elaborate because you seem to be doing this. I'll put it as an answer – Jeremy Thompson Feb 19 '13 at 23:54
  • I've added the error information as well as the offending code as asked. As for the populating bit, that's what's bothering me so much, as far as I can tell, it should work. That being said, I'm more than sure that some minor mishap is to blame. – user2088808 Feb 20 '13 at 00:05
  • Yeah see this line `DumpContents(customerGrp.Executive)` its recursively calling the DumpContents method - but with an Employee and then again in a loop with Customer type. The DumpContents must be passed a CustomerGrp. – Jeremy Thompson Feb 20 '13 at 00:08
  • Sorry, that's my fault. The DumpContents function is overloaded to take Customers, Employees, and Customer Groups. I've updated the main post with the code. Sorry! It actually produces what I need it to, and the code still produces the exception error when it is commented out. – user2088808 Feb 20 '13 at 00:14
  • I'm racking my brains on this, I'm stumped. The cust in the call to `DumpContents(cust);` cannot be Null as its in the foreach loop. This isn't a multi- threaded application is it? Also since the method is static it cannot ever produce a `Object not set to an instance` error. So how can you get that error in the loop in a static method??? – Jeremy Thompson Feb 20 '13 at 00:21
  • I haven't the slightest idea. My best guess was that the Customer list within the customer group class was null (according to debugging) and it was trying to call an empty space. If that's not it then I'm more lost than I was when I started. If I add testGrp.Customers = new list (); to the program, there is no error, but the dump of the customer data does not produce anything. – user2088808 Feb 20 '13 at 00:36

2 Answers2

0

The line DumpContents(customerGrp.Executive) its recursively calling the DumpContents method - but with an Employee type and then again in a loop with Customer type. The DumpContents must be passed a CustomerGrp.

Simply fix would be to overload the DumpContents method to dump out info for differrent types, eg:

static void DumpContents(CustomerGrp customerGrp)
{
    Console.WriteLine("------- Customer Content -------");
    Console.WriteLine("         Id: {0}", customerGrp.GroupId);
    DumpContents(customerGrp.Executive);
    foreach (Customer cust in customerGrp.Customers)
    {
        DumpContents(cust); // <- Exception Error line here
    }
    Console.WriteLine("--------------------------------");
}


static void DumpContents(Employee employee)
{
    Console.WriteLine("------- Employee Content -------");
    Console.WriteLine("         Id: {0}", employee.Id);
    ...
}


static void DumpContents(Customer customer)
{
    Console.WriteLine("------- CustomerContent -------");
    Console.WriteLine("         Id: {0}", customer.Id);
    ...
}
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
0

Make sure you initialize a new instant before the program loop testGrp.Customers = new list ();

And seem like u skip an i in loop

Edit: Wow you are good, you knew I was using my phone to answer the question.

I believe He is getting the error because his customers object was never instantiated. Typically you won't get an error on the DumpContents line because it's a foreach loop unless that object was shared between thread. You probably get the error on the previous line on this line instead: foreach (Customer cust in customerGrp.Customers)

That's why I asked him to make sure the customers objects are instantiated

But then this is just my speculation...

gavin
  • 1,276
  • 1
  • 11
  • 17
  • 1
    I wont downvote, but that doesn't make sense if `testGrp` was null the error would be on this line `DumpContents(customerGrp.Executive);` Also with the skipping an `i` that wont matter its a `foreach` not a for (int i = 0...` Finally the goal of SE sites is to become a resource of knowledge, of answers, for years to come. When you answer questions like your sending a text message you wont get upvotes. Word your answers professionally like Jon Skeet and you'll get upvotes... – Jeremy Thompson Feb 20 '13 at 00:39