System.NullReferenceException: 'Object reference not set to an instance of an object.'

LegitMeme

Member
Joined
May 5, 2019
Messages
13
Programming Experience
Beginner
Sorry if I'm stupid, I'm just a beginner.

I'm trying to make a simple program and it's not really working? If I run the application, but it only says and stops:
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=ConsoleApp1
StackTrace:
at ConsoleApp1.Program.CarMakerMaker(Int32 slot) in C:\Users\Gebruiker\source\repos\ConsoleApp1\ConsoleApp1\Program.cs:line 61
at ConsoleApp1.Program.CarMakerMenu() in C:\Users\Gebruiker\source\repos\ConsoleApp1\ConsoleApp1\Program.cs:line 44
at ConsoleApp1.Program.Main(String[] args) in C:\Users\Gebruiker\source\repos\ConsoleApp1\ConsoleApp1\Program.cs:line 14

Here is the code:
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        private static bool display = true, slotsChanged = false;
        static void Main(string[] args)
        {
            CarMakerMenu();
        }

        public static void CarMakerMenu()
        {
            string[] availible = new string[10];
            if (slotsChanged == false)
            {
                availible[0] = "availible";
                availible[1] = "availible";
                availible[2] = "availible";
                availible[3] = "availible";
                availible[4] = "availible";
                availible[5] = "availible";
                availible[6] = "availible";
                availible[7] = "availible";
                availible[8] = "availible";
                availible[9] = "availible";
            }
            while (display == true)
            {
                Console.WriteLine("Welcome to car manager!");
                Console.WriteLine("\nPick a slot \n1 {0}, \n2 {1}, \n3 {2}, \n4 {3}, \n5 {4}, \n6 {5}, \n7 {6}, \n8 {7}, \n9 {8}, \n10 {9}\nOr type 0 to exit\n",
                    availible[0], availible[1], availible[2], availible[3], availible[4], availible[5], availible[6], availible[7], availible[8], availible[9]);
                int chosenSlot = Convert.ToInt32(Console.ReadLine());

                if (chosenSlot >= 1 || chosenSlot <= 10)
                {
                    slotsChanged = true;
                    chosenSlot = chosenSlot - 1;
                    CarMakerMaker(chosenSlot);
                    chosenSlot = 0;
                }
                if (chosenSlot == 0)
                {
                    break;
                }
                else
                {
                    Console.WriteLine("Please enter a valid number");
                    return;
                }
            }
        }
        public static void CarMakerMaker(int slot)
        {
            Car[] myCar = new Car[10];
            myCar[slot].Make = "test";
            myCar[slot].Model = "test";
            myCar[slot].Year = 1043;
            myCar[slot].Color = "test";

            Console.WriteLine("{0}\n{1}\n{2}\n{3}", myCar[slot].Make, myCar[slot].Year, myCar[slot].Model, myCar[slot].Color);
            Console.ReadLine();
        }

    }

    class Car
    {
        public string Make { get; set; }
        public string Model { get; set; }
        public int Year { get; set; }
        public string Color { get; set; }

    }
}

Thanks for your help!
 
You have to create and assign a Car object before you can use it.
 
No, here you are creating an array with room for 10 Car objects.
 
Don't I create a new Car object when the line
C#:
Car[] myCar = new Car[10];
gets executed?
I'm a bit confused.
If you make an egg carton, does that mean that it's full of eggs?
 
If you make an egg carton, does that mean that it's full of eggs?
No, here you are creating an array with room for 10 Car objects.
These are the vaguest replies I've ever gotten, can you explain what you mean please?

Edit:
Aren't I making a car here?
C#:
            myCar[slot].Make = "test";
            myCar[slot].Model = "test";
            myCar[slot].Year = 1043;
            myCar[slot].Color = "test";

I'm trying to follow a course and I don't really understand some parts. Is it possible for you to explain in more detail, I would really appreciate it.
 
Last edited:
Use the new keyword to create a new Car object, just like you did when you created a new array. Here you could think of the array as a garage where you can park cars :)
 
These are the vaguest replies I've ever gotten, can you explain what you mean please?
You may well get my meaning now but, just in case, I was saying that having an egg carton doesn't mean that you have eggs in it, just as having a Car array doesn't mean that you have Cars in it. Just like an egg carton, an array is just a container with places to put a specific number of objects. You still have to put those objects there.

You can basically think of each element of an array as a variable. Just as a Car variable would be Nothing by default and you would have to use the New keyword to create a car to assign to it, so you need to do so for each element of your array.
 
i have the same error and im so confused, do you remember what you did to solve it?
You may have the same error message but it's unlikely that you have the same error. A NullReferenceException is one of the easiest exceptions to diagnose, if you would just use the debugging tools available to you. The debugger will break when the exception is thrown. Simply examine each reference on the offending line and find the one that is null. You can then work backwards through the code to find where you expected that reference to be set. If it's not, set it. If it is, set a breakpoint there and then run the project again and see whether the breakpoint is hit. If it's not, that's the issue you need to fix. If it is, you can then step through the code to see where it is reset. If you still need help solving the problem then you can ask but you need to have done all this testing and debugging first and tell us exactly where in the code the issue is, and do so in your own thread dedicated to that issue.

If you don't know how to debug then stop what you're doing and learn that first, because it's a required skill for all developers, regardless of experience level.
 
Back
Top Bottom