The problem is with your object, not with the string. When you create the array, you just created it, you forgot to initialize the objects in the array.
Movie[] m1 = new Movie[10];
But when you try to access the items from it, like,
m1[i].Title = Convert.ToString(Console.ReadLine());
It throws that error. It is OK, and that is the expected behavior, because you have not actually initialized the object. The object that is at
ith index in
m1
. To fix this problem, you would need to initialize the objects and then continue to work with the code.
Try this code instead,
for (int i=0; i < qty; i++)
{
if(m1[i] == null) {
m1[i] = new Movie();
}
Console.WriteLine("Movie #{0} Title: ", i+1);
m1[i].Title = Convert.ToString(Console.ReadLine());
Now, when you will use this, it won't throw that error because the object is not null anymore. This NullReferenceException is a very basic error found in programs when beginners try to start with their journey. :-)
What is a null error in code Execution[
^]
Edit:
As already mentioned and you know, the constructor in your class requires three parameters, in my code you need to have a parameterless constructor defined in the class. Like this,
class Movie {
public Movie() {}
}
This way, the code provided above would work, successfully. :-)