Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
1.80/5 (3 votes)
See more:
How can i convert a string to a object class in c#?

I have created a class with string variables but do not accept the value.

ERROR: Object reference not set to an instance of an object

This is what I have:

C#
using System;

namespace movies
{
    class Program
    {
        static void Main(string[] args)
        {   

            Movie[] m1 = new Movie[10];
            int qty;

            Console.WriteLine("How many Movies you want to add?");
            qty = Convert.ToInt32(Console.ReadLine());
            
            for (int i=0; i < qty; i++)
            {                
                Console.WriteLine("Movie #{0} Title: ", i+1);
                m1[i].Title = Convert.ToString(Console.ReadLine());
                    Console.WriteLine("Movie #{0} Director: ", i+1);
                m1[i].Director = Console.ReadLine();
                Console.WriteLine("Movie #{0} Year: ", i+1);
                m1[i].Year = Convert.ToInt32(Console.ReadLine());
            }
        
        }// end of main
     }//end of class program
     
    class Movie 
    {
        string title = "V-for vendetta";
        string director = "Portman";
        int year = 2002;
        
        public string Title
        {
            set { title = value; }
            get { return title; }
        }
        
        public string Director
        {
            set { director = value; }
            get { return director; }
        }    
               
        public int Year
        {
            set { year = value; }
            get { return year; }
        }
        
        public Movie (string a, string b, int c)
        {
            Title = a;
            Director = b;
            Year = c;
        }
        
        public void print()
        {
            Console.WriteLine("Title: {0}", Title);
            Console.WriteLine("Director: {0}", Director);
            Console.WriteLine("Year: {0}", Year);
        }
    }//End of class class movies
    
    
}//end of namespace
Posted
Updated 16-Dec-15 7:34am
v3

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.
C#
Movie[] m1 = new Movie[10];

But when you try to access the items from it, like,
C#
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,

C#
for (int i=0; i < qty; i++)
{                
    if(m1[i] == null) {
        m1[i] = new Movie(); // Create the object
    }
    Console.WriteLine("Movie #{0} Title: ", i+1);
    m1[i].Title = Convert.ToString(Console.ReadLine());
    // So on...

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,
C#
class Movie {
   // Fields

   public Movie() {}

   // Other constructors and functions
}

This way, the code provided above would work, successfully. :-)
 
Share this answer
 
v2
Comments
Leo Chapiro 16-Dec-15 10:17am    
The using of the static array [10] is the big error as well: what if the user choose qty = 12?
Afzaal Ahmad Zeeshan 16-Dec-15 10:21am    
I did not recommend him to use the array, infact in my opinion using classic arrays is also not a good approach. He should consider using List, such as List<Movie>.

But that had nothing to do with the error and the topic of question. Problem was object being null, not being out of index. :-)
Leo Chapiro 16-Dec-15 10:38am    
This is not all what is wrong in your answer: new Movie() will not work because the c'tor of "Movie" needs three arguments. We can't fix one error by leaving several other back !
Afzaal Ahmad Zeeshan 16-Dec-15 13:54pm    
Missed that constructor out, fixed it. :-)

Thanks.
BillWoodruff 16-Dec-15 13:27pm    
+4 all you need to do get my #5 is mention that your code will require the 'Movie Class to have a parameter-less constructor :)
Static array is very bad, you assume the size will be under 10 but it must not!
Take the List as dynamically growing container, try this:

C#
static void Main()
{
    //Movie[] m1 = new Movie[10];
    List<Movie> mi = new List<Movie>();
    int qty;

    Console.WriteLine("How many Movies you want to add?");
    qty = Convert.ToInt32(Console.ReadLine());

    for (int i = 0; i < qty; i++)
    {

        Console.WriteLine("Movie #{0} Title: ", i + 1);
        string sTitle = Convert.ToString(Console.ReadLine());
        Console.WriteLine("Movie #{0} Director: ", i + 1);
        string sDirector = Console.ReadLine();
        Console.WriteLine("Movie #{0} Year: ", i + 1);
        int nYear = Convert.ToInt32(Console.ReadLine());

        mi.Add(new Movie(sTitle, sDirector, nYear));

    }
}
 
Share this answer
 
v3

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