Hello,
I was wondering if I could get a little guidance regarding an error I keep running into on this assignment. I'm not necessarily looking for any answers as I'd like to complete as much of the assignment myself as possible. However, I've been beating my head against a brick wall on this one error, so if someone can point out why I'm getting this error and what I can do to avoid it, it would be greatly appreciated!
The assignment focuses on argumentExceptions and formatExceptions. In my code, I've been able to provide an output for the correct exceptions, but when attempting to output my results, I get a NullReferenceException in line 39.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Exceptional
{
class WorkerTest
{
static void Main(string[] args)
{
Worker[] workerArray = new Worker[5];
int workerID;
double hrSal;
for (int x = 0; x < workerArray.Length; x++)
{
try
{
Console.Write("Input a work identification number: ");
workerID = Convert.ToInt32(Console.ReadLine());
Console.Write("Input an hourly Salary for the worker: ");
hrSal = Convert.ToDouble(Console.ReadLine());
workerArray[x] = new Worker(workerID, hrSal);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
Console.WriteLine();
for (int i = 0; i < workerArray.Length; i++)
{
Console.WriteLine("Worker # {0} \nHourly salary {1}\n--------\n ", workerArray[i].workerID, workerArray[i].hrSal.ToString("C"));
}
}
}
class Worker
{
public int workerID;
public double hrSal;
public Worker(int WorkerID, double HrSal)
{
workerID = WorkerID;
hrSal = HrSal;
if (hrSal < 5.0 || hrSal > 55.0)
{
workerID = 123;
hrSal = 7.75;
throw new ArgumentException("Unexpected Range.");
}
}
}
}
I have attached to this post the requirements for the assignment.