-1

I was learning how to use Json to save data into files when a continuous error was appearing, its "Nullreference, object reference not set to an instance of an object" and I started to think that I was doing the save file part right but the error was in my usage of arrays somehow, so I started a new project and I started using arrays the same way without any usage of Json, and indeed the error was there, so I know there is something wrong with this but I cant really tell what, I know how simple arrays are initialized and how every array has to be initialized before being used, but in this case something is wrong for some reason, can you tell me what is that?

The scenario is really simple as its new project only to test the arrays the way I was using them in my bigger project, so its mainly a GameClass that hold several hi-scores tables (an array of them), these tables are made by a class (HiScoreClass) that holds arrays of names and points, and then well I was just initializing this and the error appeared, so no need to dig further, can you tell me what is wrong here? thanks a lot for your help, Im totally lost in this point. (To test this in a project I just attached the code to the camera so its nothing else in scene to think other object could be causing it)

using UnityEngine;
 using System.Collections;

 public class embed : MonoBehaviour
 {
     GameClass myGame;
     int i, j;
     void Awake ()
     {
         myGame = new GameClass();
         myGame.arrptnm = new HiScoreClass[10];
         for (i = 0; i < 100; i++)
         {
             myGame.arrptnm[i].pts = new int[10];
             myGame.arrptnm[i].names = new string[10];
             for (j = 0; j < 10; j++)
             {
                 myGame.arrptnm[i].pts[j] = i * j;
                 myGame.arrptnm[i].names[j] = "ASD";
             }
         }
     }

     void Update ()
     {

     }
 }

 [System.Serializable]
 class GameClass
 {
     [SerializeField]
     public HiScoreClass[] arrptnm;
 }
 [System.Serializable]
 class HiScoreClass
 {
     [SerializeField]
     public int[] pts;
     public string[] names;
 }
Ross
  • 1,298
  • 4
  • 16
  • 24
asdronin
  • 45
  • 8

1 Answers1

1

The problem is that you're defining an array of HiScoreClass objects, but you're not initializing the elements in that array to new instances of the HiScoreClass class. Then you get a NullReferenceException when you try to reference a property of an item in the array, like myGame.arrptnm[i].pts.

To solve this, you can initialize each item to a new instance when you do your first iteration (also change the 100 to 10, since that's the size we declared for this array):

        for (int i = 0; i < 10; i++)
        {
            // Initialize our array items
            myGame.arrptnm[i] = new HiScoreClass();

            // rest of code omitted...
Rufus L
  • 32,853
  • 5
  • 25
  • 38
  • I see, thanks a lot, I was totally missing that initializing there, you saved me from a continous headache =) – asdronin Jun 18 '18 at 18:41