Search Unity

Array and object reference exception

Discussion in 'Scripting' started by Roccc_y, May 5, 2021.

  1. Roccc_y

    Roccc_y

    Joined:
    Jul 14, 2020
    Posts:
    5
    Im having issues with my script. The error message is "NullReferenceException: Object reference not set to an instance of an object", as always, but I cant figure out what the problem is. First of all, with my code I try to calculate what exact weekday any day in history is. The problem has probably something to do with the array. I would be glad, if anyone helps me figure it out. I am using Rider for coding btw. and sorry for you having to read this long amateur code...
    Code (CSharp):
    1. public class GameTime : MonoBehaviour
    2. {
    3.     public GameObject TimeAd;
    4.     private TextMeshProUGUI TimeAdText;
    5.    
    6.     private date GameDate;
    7.     private date DayCal;
    8.  
    9.     private string comp;
    10.     private int[] MonthCalc;
    11.     private bool leapYear;
    12.  
    13.     private Timer time;
    14.     public float interval;
    15.  
    16.     private int DayNumber;
    17.     private int MonthNumber;
    18.     private int YearNumber;
    19.     private int centuryNumber;
    20.     private int leapNumber;
    21.     private string yearF;
    22.     private string yearR;
    23.    
    24.  
    25.     private void Start()
    26.     {
    27.         TimeAdText = TimeAd.GetComponent<TextMeshProUGUI>();
    28.        
    29.         int[] MonthCalc = new int[11];
    30.         MonthCalc = new[] {0, 3, 3 ,6 ,1, 4, 6, 2, 5, 0, 3, 5};
    31.  
    32.         GameDate = new date();
    33.         DayCalculation();
    34.        
    35.         time.Elapsed += OnTimerEvent;
    36.         time.Interval = (interval * 60000) / Time.deltaTime;
    37.         time.Enabled = true;
    38.     }
    39.  
    40.     private void Update()
    41.     {
    42.         switch (GameDate.day)
    43.         {
    44.             case 1:
    45.                 TimeAdText.text = GameDate.DayName + " " + GameDate.day + "st" + ", " + (date.MonthE)GameDate.month + " " + GameDate.year;
    46.                 break;
    47.             case 2:
    48.                 TimeAdText.text = GameDate.DayName + " " + GameDate.day + "nd" + ", " + (date.MonthE)GameDate.month + " " + GameDate.year;
    49.                 break;
    50.             case 3:
    51.                 TimeAdText.text = GameDate.DayName + " " + GameDate.day + "rd" + ", " + (date.MonthE)GameDate.month + " " + GameDate.year;
    52.                 break;
    53.             default:
    54.                 TimeAdText.text = GameDate.DayName + " " + GameDate.day + "th" + ", " + (date.MonthE)GameDate.month + " " + GameDate.year;
    55.                 break;
    56.         }
    57.     }
    58.     private void OnTimerEvent(Object source, ElapsedEventArgs e)
    59.     {
    60.         GameDate.day += 1;
    61.         if (GameDate.DayName == "Sat")
    62.         {
    63.             GameDate.DayName = "" + (date.DayAbrev)0;
    64.         }
    65.         else
    66.         {
    67.             for (int i = 0; i < 6; i++)
    68.             {
    69.                 comp = "" + (date.DayAbrev)i;
    70.                 if (comp.Equals(GameDate.DayName))
    71.                 {
    72.                     GameDate.DayName = "" + (date.DayAbrev)i+1;
    73.                 }
    74.             }
    75.         }
    76.        
    77.         if (GameDate.month == 2 && GameDate.day > 28)
    78.         {
    79.             GameDate.day = 1;
    80.             GameDate.month += 1;
    81.         } else if ((GameDate.month == 4 || GameDate.month == 6 || GameDate.month == 9 || GameDate.month == 11) && GameDate.day > 30)
    82.         {
    83.             GameDate.day = 1;
    84.             GameDate.month += 1;
    85.         } else if (GameDate.day > 31)
    86.         {
    87.             GameDate.day = 1;
    88.             GameDate.month += 1;
    89.         }
    90.  
    91.         if (GameDate.month > 12)
    92.         {
    93.             GameDate.month = 1;
    94.             GameDate.year += 1;
    95.         }
    96.     }
    97.  
    98.     private void DayCalculation()
    99.     {
    100.         DayCal = new date(GameDate);
    101.         DayNumber = DayCal.day % 7;
    102.        
    103.         MonthNumber = 0;
    104.         for (int i = 0; i <= 11; i++)
    105.         {
    106.             comp = "" + (date.MonthE)i;
    107.             if (comp.Equals("" + (date.MonthE)GameDate.month))
    108.             {
    109.                 MonthNumber = MonthCalc[i];
    110.             }
    111.         }
    112.         yearF = ("" + DayCal.year).Substring(0, 2);
    113.         yearR = ("" + DayCal.year).Substring(2);
    114.         YearNumber = int.Parse(yearR) + (int.Parse(yearR) / 4) % 7;
    115.         centuryNumber = (3 - (int.Parse(yearF) % 4)) * 2;
    116.         leapNumber = 0;
    117.         if (DayCal.year % 2 != 0 && (DayCal.month == 0 || DayCal.month == 1))
    118.         {
    119.             leapNumber = 6;
    120.         }
    121.         int DayEndNumber = (DayNumber + MonthNumber + YearNumber + centuryNumber + leapNumber) % 7;
    122.         DayCal.DayName = "" + (date.DayAbrev)DayEndNumber;
    123.         GameDate.DayName = DayCal.DayName;
    124.     }
    125. }
    126.  
    127. public class date : MonoBehaviour
    128. {
    129.     public int day;
    130.     public int month;
    131.     public int year;
    132.     public string DayName;
    133.  
    134.     public date()
    135.     {
    136.         day = Random.Range(1, 28);
    137.         month = Random.Range(1, 11);
    138.         year = Random.Range(1000, 2000);
    139.     }
    140.    
    141.     public date(date copy)
    142.     {
    143.         day = copy.day;
    144.         month = copy.month;
    145.         year = copy.year;
    146.     }
    147.  
    148.     public enum MonthE
    149.     {
    150.         January = 1,
    151.         February = 2,
    152.         March = 3,
    153.         April = 4,
    154.         May = 5,
    155.         June = 6,
    156.         July = 7,
    157.         August = 8,
    158.         September = 9,
    159.         October = 10,
    160.         November = 11,
    161.         December  = 12
    162.     }
    163.  
    164.     public enum DayAbrev
    165.     {
    166.         Son = 0,
    167.         Mon = 1,
    168.         Tue = 2,
    169.         Wed = 3,
    170.         Thu = 4,
    171.         Fri = 5,
    172.         Sat = 6
    173.     }
    174. }
    the console output:
    NullReferenceException: Object reference not set to an instance of an object
    GameTime.DayCalculation () (at Assets/Scripts/GameTime.cs:117)
    GameTime.Start () (at Assets/Scripts/GameTime.cs:41)
     

    Attached Files:

    Last edited: May 5, 2021
  2. vargata

    vargata

    Joined:
    Nov 26, 2013
    Posts:
    120
    would be much easier to insert your code as text: insert code.png
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Post code using code tags.

    Copy and paste your error. It contains important information such as what script has the error and line numbers.

    Help us to help you. :)
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    It actually doesn't matter what the code is or what you are trying to do. You never have to post code for a nullref. Why?

    Because the answer is always the same... ALWAYS. It is the single most common error ever.

    Don't waste your life spinning around and round on this error. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception
    - also known as: Object reference not set to an instance of an object

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.
     
  5. Roccc_y

    Roccc_y

    Joined:
    Jul 14, 2020
    Posts:
    5
    sorry, didnt see it.
     
  6. Roccc_y

    Roccc_y

    Joined:
    Jul 14, 2020
    Posts:
    5
    thats the error line
     

    Attached Files:

  7. Roccc_y

    Roccc_y

    Joined:
    Jul 14, 2020
    Posts:
    5
    didn not see it at first glance, sorry. I have uploaded the error line now
     
  8. Roccc_y

    Roccc_y

    Joined:
    Jul 14, 2020
    Posts:
    5
    first of all thanks for your help, but still i dont know whats the real problem with the code. Since the console shows me which line contains a error, I know it has do to something with Int Monthnumber being set to a value of the array, but I just dont get it.
    Also I tried to look at which part of the function messes up the whole thing, because there is no real output, and the for-loop kind of ends the function. I hope Im not annoying you with my problems
     
  9. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    It's simple. Line 117, which references your array. MonthCale? MonthCalc? I can't tell the exact spelling. So then you need to determine why is it null. So you look to where you declare it. It is declared as a class variable, but not initialized there. So next, look and see where you initialize it. Ok, here is your problem. You actually don't initialize it.

    Instead, you try to initialize it in Start, but instead you create a new array in Start that you initialize as a local variable just to Start. Which you then populate that local variable in Start. So your class variable stays empty and uninitialized and is thus null.

    Thus in Start you need to remove int[] so that you target the class variable instead of creating a new local variable.

    @Kurt-Dekker post is basically directing you to this basic troubleshooting steps. :) He's trying to help you debug a null issue, which is super common, so learning how to handle it will only serve you well in the future.

    Also to add, pay attention to squiggles and when code text goes grey as they can indicate important information. In this case in your Start it is trying to inform you that you made a variable but aren't doing anything with it. A little sus if you expect to be doing something with it.
     
    Roccc_y and Kurt-Dekker like this.
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Exactly... I have given up teaching people how to fix particular null references. That's just a silly pursuit.

    Instead, I teach you how to fix this particular null reference... and EVERY SINGLE NULL REFERENCE YOU WILL EVER ENCOUNTER FOR THE REST OF YOUR LIFE!

    It's an incredible power you can learn too... And I do it all for free because I love you all.

    You see my dog in the avatar pic? Even he can solve nullrefs today thanks to my three-step technique. He often chews on them afterwards, but they're definitely solved.
     
    Roccc_y and Brathnann like this.