-2

the class that I've made:

 public class singleWeeklyWorking_Period 
    {   // this class holds a single period of work schedule, that can appear weekly on specific days
        public bool[] onDays = new bool[7]; // 7 days a week, set to true on days that this period appears
        public int startTime = 0; // in minutes
        public int endTime = 0;

        
        public void show() // just for testing
        {
            UnityEngine.Debug.Log("days: " + onDays[0] + ", "+ onDays[1] + ", "+ onDays[2] + ", "+ onDays[3] + ", "+ onDays[4] + ", "+ onDays[5] + ", "+ onDays[6]);
            UnityEngine.Debug.Log("startTime: " + startTime);
            UnityEngine.Debug.Log("endTime: " + endTime);
        }
    }

the code that uses the class (just for testing):

    public List<singleWeeklyWorking_Period> listOfWeeklyWorkingTime ; 
    // lists of working time


    // Start is called before the first frame update
    void Start()
    {
        singleWeeklyWorking_Period period = new singleWeeklyWorking_Period();
        period.onDays[0] = true;
        period.startTime = 60;
        period.endTime = 120;
        listOfWeeklyWorkingTime.Add(period);

        listOfWeeklyWorkingTime[0].show();
    }

as you can see in "singleWeeklyWorking_Period period = new singleWeeklyWorking_Period();". I instantiated the class. but it still gives me an error that says:

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

is it the variable inside the class? but I've set it just before I add it to the list:

period.onDays[0] = true;
period.startTime = 60;
period.endTime = 120;

I'm quite confused, all the help is appreciated!

  • 3
    Are you creating `listOfWeeklyWorkingTime ` ? – Sean Oct 22 '21 at 08:51
  • 2
    From the code you've shown it looks like the list will be null – the.Doc Oct 22 '21 at 08:52
  • As said above, you didn't instantiate the list (`listOfWeeklyWorkingTime`), therefore when you try to `.Add` something in that (`null`) list, it throws that exception – Rafalon Oct 22 '21 at 08:57

2 Answers2

2

It seems you have not initiated list.

You have this line.

public List<singleWeeklyWorking_Period> listOfWeeklyWorkingTime ;

Use this instead.

public List<singleWeeklyWorking_Period> listOfWeeklyWorkingTime = new List<singleWeeklyWorking_Period>() ;

Note: I am assuming that error is when you try to add object.

dotnetstep
  • 17,065
  • 5
  • 54
  • 72
  • Even better for a Unity context would be to simply make the class `[Serializable] public class singleWeeklyWorking_Period { ... }` this way it a) can be exposed and configured in the Inspector in Unity b) can be saved and c) the Inspector would already auto-initialize this array as part of the deserialization .... so once everything is configured in the Inspector `Start` could actually be reduced to the line `listOfWeeklyWorkingTime[0].show();` – derHugo Oct 22 '21 at 09:00
0

A class or struct definition is like a blueprint that specifies what the type can do. An object is basically a block of memory that has been allocated and configured according to the blueprint. Instances of classes are created by using the new operator so you need to use it.

 public List<singleWeeklyWorking_Period> listOfWeeklyWorkingTime = new List<singleWeeklyWorking_Period>(); 
Ran Turner
  • 14,906
  • 5
  • 47
  • 53