- Home /
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...
mergesort-3.png
(285.4 kB)
Comment