Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

2D Array System.NullReferenceException

Discussion in 'Scripting' started by James-Sullivan, Jun 17, 2015.

  1. James-Sullivan

    James-Sullivan

    Joined:
    Jun 15, 2015
    Posts:
    128
    Hello. In the class, map, there is a 2D array whose length is defined by rows and coluoms. In Map's constructor, the array is filled with the string "Empty".

    Then in the other function of the Map class, LocationIsOnMap, it receives a index and performs a check on the object that is in that index in the array. In that if statement I get a System.NullReferenceException when I try to do map[loc.x,loc.y]. I also get this error whenever I try and use map in anyway in that function. I'm confident that LocationIsOnMap is within map's scope so I don't know whats wrong here.

    Code (CSharp):
    1.     public class Map
    2.     {
    3.         protected int rows;
    4.         protected int coluoms;
    5.         protected string[,] map;
    6.         // Constructor
    7.         public Map ()
    8.         {
    9.             rows = 5;
    10.             coluoms = 5;
    11.             // A 2D array for the map with rows and coluoms for its dimensions
    12.             string[,] map = new string [rows,coluoms];
    13.  
    14.             for (int i = 0; i < rows; i++)
    15.             {
    16.                 for (int k = 0; k < coluoms; k++)
    17.                 {
    18.                     map[i,k] = "Empty";
    19.                 }
    20.             }
    21.  
    22.             for (int i = 0; i < rows; i++)
    23.             {
    24.                 for (int k = 0; k < coluoms; k++)
    25.                 {
    26.                     /*Console.WriteLine("map[i,k] = " +  map[i,k]);
    27.                     Console.WriteLine ("i = " + i);
    28.                     Console.WriteLine ("k = " + k);*/
    29.                 }
    30.             }
    31.             //Console.WriteLine(map.GetLength(0));
    32.         }
    33.  
    34.  
    35.         // Returns a bool stating if the given location is on the map
    36.         public bool LocationIsOnMap(Vector loc)
    37.         {
    38.             bool retBool = false;
    39.  
    40.             if (loc.x <= coluoms & loc.y <= rows)
    41.             {
    42.                 if (map[loc.x,loc.y] == "Empty")
    43.                 {
    44.                     retBool = true;
    45.                 }
    46.             }
    47.             return retBool;
    48.         }
    49.     }
    50.     // End of class
    51. }
    52.  
     
  2. SquarePieStudios

    SquarePieStudios

    Joined:
    Apr 22, 2015
    Posts:
    33
    line 40 uses & instead of &&. Change that and see if that helps?

    Additionally, your map building builds the rows into the columns and the columns into the rows. If your map isn't square, this will cause confusion and IndexOutOfBounds exceptions.
     
  3. James-Sullivan

    James-Sullivan

    Joined:
    Jun 15, 2015
    Posts:
    128
    That didn't fix it. If I do map.GetType() in the that function, it gives me that same error.
     
  4. SquarePieStudios

    SquarePieStudios

    Joined:
    Apr 22, 2015
    Posts:
    33
    What happens if you uncomment out line 31? Is that called before the NRE? From the code provided, there should never be a case where map is null as it is instantiated within the provided constructor.
     
  5. James-Sullivan

    James-Sullivan

    Joined:
    Jun 15, 2015
    Posts:
    128
    Uncommenting 31 didn't cause any problems. Same with uncommenting 26-28. What does this error mean exactly?
     
  6. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You're never actually instantiating your 2D array. This line is creating a new map variable local to your constructor's scope:
    Code (csharp):
    1.  
    2. string[,] map = new string [rows,coluoms];
    3.  
    Delete the string[,] portion to avoid this:
    Code (csharp):
    1.  
    2. map = new string[rows, coluoms];
    3.  
     
    SquarePieStudios and LeftyRighty like this.
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    did you mean to use the Vector class/struct from the c# core libraries rather than unity's Vector2/3/4 classes?
     
  8. James-Sullivan

    James-Sullivan

    Joined:
    Jun 15, 2015
    Posts:
    128
    Thats fixes it, thanks to all.

    I actully couldn't figure out how to include the namespace C#'s vector was in so I made my own struct that has 2 public fields, x and y. Its good enough for my uses. Whats the difference between C#'s vector and Unity's vector and how do I include their namespaces?
     
  9. SquarePieStudios

    SquarePieStudios

    Joined:
    Apr 22, 2015
    Posts:
    33
    Rarg! How did I not see that!