-1

Im trying to build a simple console app called Digital Caddie. The purpose is that the user should have a maximum of 5 bags and each bag should contain a maximum of 14 clubs.

We have created a class called "klubba".

class Klubba
{        
        public string klubbNamn;
        public int maxLängd; (maxLength)
        public int minLängd; (minLength)    
}

and a class called "bag"

class Bag
{
    public string namn;
    public Klubba[] klubba;
}

The problem we got is that when we are trying to add a value to each varible we get a message saying: "System.NullReferenceException: 'Object reference not set to an instance of an object.' i was 'null' " below you can find the rest of our code. The problem accours when we are reaching the method "LäggTillBag".

namespace Digital_Caddie
{
    class Program
    {
        static Bag[] bagRegister = new Bag[0];
        static void Main(string[] args)
        {
            Huvudmeny();
            SkrivUtBaglista(bagRegister);

        }
        static void Huvudmeny()
        {

            while (true)
            {
                Console.WriteLine("\nVälkommen till Huvudmenyn, väl ett av nedan alternativ! \n Kom ihåg att skapa din bag innan du kan börja spela:)");
                Console.WriteLine("1. Spela Golf");
                Console.WriteLine("2. Skapa ny bag");
                Console.WriteLine("3. Mina bags");
                Console.WriteLine("4. Avsluta programmet");

                string användarInput = Console.ReadLine();

                if (användarInput == "1")
                {
                    Console.WriteLine("Dags att spela golf! \n Välj din bag för att komma igång.");

                }
                else if (användarInput == "2")
                {
                    Console.WriteLine("Dags att skapa ny bag, kör hårt!");
                    LäggTillBag();


                }
                else if (användarInput == "3")
                {
                    Console.WriteLine("Mina bags");

                }
                else if (användarInput == "4")
                {
                    Console.WriteLine("Du har nu avslutat programmet, välkommen åter!");
                    break;
                }
                else
                {
                    Console.WriteLine("Ogiltligt val, välj mellan 1-4!");
                }
            }
        }

        public static void LäggTillBag() //Prompta användaren till att skapa en ny bag med klubbor.
        {
            Bag ny = new Bag();
            ny.klubba = new Klubba[14];
            
            Console.WriteLine("Döp din bag: ");
            ny.namn = Console.ReadLine();
            uint antalKlubbor = ReadUInt("Hur många klubbor vill du lägga till i bagen?: ");

            for (int i = 0; i < antalKlubbor; i++)
            {
                //Får felmeddelande när attribut för klubbans namn,max och min längd ska lägga in i arrayn. Den vill inte köpa attributen.
                Console.WriteLine("#" + (i + 1) + "Namnge ny klubba: \n");
                ny.klubba[i].klubbNamn = Console.ReadLine();

                
                Console.WriteLine("#" + (i + 1) + "Ange max längd som du slår med klubban: \n"); 
                ny.klubba[i].maxLängd = int.Parse(Console.ReadLine());



                Console.WriteLine("#" + (i + 1) + "Ange minimum längd som du slår med klubban: \n");
                ny.klubba[i].minLängd = int.Parse(Console.ReadLine());


            }
            bagRegister = UtökaBagRegister(bagRegister, ny); //Lägger till bag sist i bagregister.
            
        }
        public static Bag[] UtökaBagRegister(Bag[] lista, Bag ny)
        {
            Bag[] nyLista = new Bag[lista.Length + 1];
            for (int i = 0; i < lista.Length; i++)
                nyLista[i] = lista[i];
            nyLista[lista.Length] = ny;
            return nyLista;

        }
        public static uint ReadUInt(string label)
        {
            Console.WriteLine(label);
            uint tal;
            while (!uint.TryParse(Console.ReadLine(), out tal))
            {
                Console.WriteLine("Måste vara ett possitivt tal! ");
                Console.WriteLine(label);
            }

            return tal;
        }

        public static void SkrivUtBaglista(Bag[] bagRegister)
        {
            Console.WriteLine("Utskrift av bagregister: \n");
            for (int i = 0; i < bagRegister.Length; i++) //loopa igenom bagregistret
            {
                Console.WriteLine(bagRegister.Length);
                Console.WriteLine("\n#" + i + "Bagnamn: " + bagRegister[i].namn);
                Console.WriteLine("Klubbor i baggen: " + bagRegister[i].klubba);

                /*for (int k = 0; k < bagRegister.Length; k++)
                {
                    Console.WriteLine("Klubba: " + bagRegister[i].klubbNamn + "denna klubbar går mellan " + bagRegister[i].minLängd + "-" + bagRegister[i].maxLängd + " m");
                }*/
                
                /*foreach (string klubba in bagRegister[i].klubbNamn)
                {
                    
                    Console.Write(klubba + "\t\t");
                }
                Console.Write("\nmin:");
                foreach (int minLängd in bagRegister[i].minLängd)
                {
                    Console.Write(minLängd + "\t\t");
                }
                Console.Write("\nmax:{0,3} ");
                foreach (int maxLängd in bagRegister[i].maxLängd)
                {
                    Console.Write(maxLängd + "\t\t");
                }*/
            }
        }

    }
    
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • 1
    tip: do not use special characters when writing code (unless they are strings) – Stefano Nardo Feb 17 '22 at 11:59
  • 2
    you're initialising a `Klubba`-array with a _capacity_ of 14. but it does not have any content - it's a _bunch of empty boxes_. you have to assign _values_ first. – Franz Gleichmann Feb 17 '22 at 12:00
  • @StefanoNardo There's nothing "special" about `ö` or `ä` in Swedish. – Mathias R. Jessen Feb 17 '22 at 12:01
  • @MathiasR.Jessen No matter where you are on this planet, anything beyond ASCII is destined to be misinterpreted or munged at least once in its lifetime. Cold hard reality; even in 2022. – Ruud Helderman Feb 17 '22 at 12:21
  • @MathiasR.Jessen they are not part of ASCII printable characters, I would avoid to use them in my code to be sure that the compiler reads it correctly. – Stefano Nardo Feb 17 '22 at 13:41

1 Answers1

1

Initializing an array is not enough to access it's items - you have to create objects first.

  1. ny.klubba = new Klubba[14]; //[null,null,null,null,null...]

  2. ny.klubba[i] = new Klubba(); // this is missing

  3. ny.klubba[i].klubbNamn = Console.ReadLine();

fubo
  • 44,811
  • 17
  • 103
  • 137