Reputation: 166
I have a problem with an array of classes. basically I create an array of the class 'Student', but I cannot assign values to any properties of the instances in the array because I get the unhandled exception in the title. here's the code:
class ControllerQueue
{
model.ModelStudent q = new model.ModelStudent();
static int capacity = 15;
model.ModelStudent[] arr = new model.ModelStudent[capacity];
int top = -1, rear = 0;
public ControllerQueue()
{
arr[0].name = "a";
arr[0].grade = 0;
}
}
I tried assigning values out of the constructor, but I still get the same result. now, the exception itself obviously sais I didn't instantiate the Student class but I don't understand why it would say that, I've already instantiated it. thanks in advance.
Upvotes: 0
Views: 205
Reputation: 39278
You need
arr[0] = new ModelStudent();
arr[0].name = "a";
arr[0].grade = 0;
You need this because you have to new
up an instance to put into the array at index 0
model.ModelStudent[] arr = new model.ModelStudent[capacity];
Will just allocate the array, but each entry is by default the default value of ModelStudent (null)
Upvotes: 1
Reputation: 12683
Your item 0 is not set.
Try.
model.ModelStudent q = new model.ModelStudent();
static int capacity = 15;
model.ModelStudent[] arr = new model.ModelStudent[capacity];
int top = -1, rear = 0;
public ControllerQueue()
{
arr[0] = new model.ModelStudent();
arr[0].name = "a";
arr[0].grade = 0;
}
Upvotes: 1
Reputation: 31196
you need to instantiate the members of your array
add the item to your array like this
arr[0] = new ModelStudent();
arr[0].name = "a";
arr[0].grade = 0;
Upvotes: 1