Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by HumblPi · May 09, 2017 at 05:21 AM · c#nullreferenceexceptionarrayssorting

MergeSort function acting strange

Hello (again) all!

I'm having a strange issue with my script. In this script, I sort an Array of RawImages using the corresponding Location (an original class including just row, column, and height). However, every time I run the program, the very first location in the Location array is always set to null, and it's only found to be null within the DoMerge method.

This is the MergeSort algorithm I'm using and the method in which I'm using it. L:

 public void MergeSort_Recursive (RawImage[] imgs, Location[] locs, int left, int right)
 {
     print ("Merge sorting list of length " + locs.Length + " from " + left + " to " + right);
     int mid;

     if (right > left)
     {
         mid = (right + left) / 2;

         if (locs [0] == null) {
             throw new NullReferenceException ("NULL AT " + 0);
         } else {
             print ("0 LOC " + locs.Length + "IS " + locs [0]);
             MergeSort_Recursive(imgs, locs, left, mid);
             MergeSort_Recursive(imgs, locs, (mid + 1), right);

             DoMerge(imgs, locs, left, (mid + 1), right);
         }


     }
 }

 public void DoMerge(RawImage[] imgs, Location[] locs, int left, int mid, int right)
 {

     if (locs [0] == null) {
         throw new NullReferenceException ("NULL AT " + 0);
     }

     RawImage[] temp = new RawImage [25];
     Location [] tempLocs = new Location[25];
     int i, left_end, num_elements, tmp_pos;

     left_end = (mid - 1);
     tmp_pos = left;
     num_elements = (right - left + 1);

     while ((left <= left_end) && (mid <= right))
     {
         if (locs [left] == null) {
             for (int p = 0; p < locs.Length; p ++) {
                 print (locs [p]);
                 throw new NullReferenceException ("locs [" + left + "] is null");
             }
         }

         if (locs [left].Row <= locs [mid].Row) {
             temp [tmp_pos++] = imgs [left++];
             tempLocs [tmp_pos++] = locs [left++];
         } else {
             temp [tmp_pos++] = imgs [mid++];
             tempLocs [tmp_pos++] = locs [mid++];

         }
     }

     while (left <= left_end) {
         temp [tmp_pos++] = imgs [left++];
         tempLocs [tmp_pos++] = locs [left++];
     }
     while (mid <= right) {
         temp [tmp_pos++] = imgs [mid++];
         tempLocs [tmp_pos++] = locs [mid++];
     }

     for (i = 0; i < num_elements; i++)
     {
         imgs[right] = temp[right];
         locs [right] = tempLocs [right];
         right--;
     }
 }

 public void sortAllObjects ()
 {
     ArrayList allObs = new ArrayList ();
     ArrayList allLocs = new ArrayList ();

     ArrayList tiles = new ArrayList (),
     tileLocs = new ArrayList ();


     Location loc, lastLoc = null;
     RawImage img;



     for (int i = 0; i < battleMap.Roster.Count; i++) {
         if (((Player)battleMap.Roster [i]).currentLocation () != null) {
             allObs.Add (((Player)battleMap.Roster [i]).MapSprite);
             allLocs.Add (((Player)battleMap.Roster [i]).currentLocation ());
         }
     }

     if (allLocs [0] == null) {
         throw new NullReferenceException ("DAFUQ?! COME ON MAN!");
     }

     for (int i = 0; i < miniMap.Length; i++) {
         for (int j = 0; j < miniMap [0].Length; j++) {
             loc = new Location (i, j);

             if (battleMap.heightOf (loc) > 0) {
                 tiles.Add (tileMap [i] [j]);
                 tileLocs.Add (new Location (i, j));
             }

             if (!battleMap.isEmpty (loc) && !battleMap.objectAt (loc).sentient ()) {
                 allObs.Add (miniMap [i] [j]);
                 allLocs.Add (new Location (i, j));
             }
             if (!battleMap.isEmptyBenign (loc) || !battleMap.isEmptyInteractable (loc)) {
                 allObs.Add (interMap [i] [j]);
                 allLocs.Add (new Location (i, j));
             }
         }
     }

     RawImage[] imgs = new RawImage[allObs.Count];
     Location[] locs = new Location[imgs.Length];

     for (int i = 0; i < imgs.Length; i++) {
         imgs [i] = (RawImage)allObs [i];
         locs [i] = (Location)allLocs [i];
     }
     if (locs [0] == null) {
         throw new NullReferenceException ("NULL AT " + 0);
     }

     MergeSort_Recursive (imgs, locs, 0, imgs.Length - 1);
     for (int i = 0; i < imgs.Length; i++) {
         imgs [i].transform.SetAsFirstSibling ();
     }

     imgs = new RawImage[tiles.Count];
     locs = new Location[imgs.Length];

     for (int i = 0; i < imgs.Length; i++) {
         imgs [i] = (RawImage)tiles [i];
         locs [i] = (Location)tileLocs [i];
     }
     MergeSort_Recursive (imgs, locs, 0, imgs.Length - 1);
     for (int i = 0; i < imgs.Length; i++) {
         imgs [i].transform.SetAsFirstSibling ();
     }


     for (int i = 0; i < allLocs.Count; i++) {
         ((RawImage)allObs [i]).transform.SetAsFirstSibling ();
     }

     for (int i = 0; i < tiles.Count; i++) {
         ((RawImage)tiles [i]).transform.SetAsFirstSibling ();
     }

     groundTexture.transform.SetAsFirstSibling ();

 }

I'm having zero issues with the other parts, but as soon as I run the program...

alt text

mergesort-3.png (285.4 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Sorting Collider Arrays by location of elements 1 Answer

[Solved] Actually totally explainable NRE with 2D array 0 Answers

Null reference when accessing GameObject in the Array(C#) 1 Answer

Vector3 resultant array sorting 2 Answers

NullReference when accessing GameObject in array (C#) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges