- Home /
Null reference exception in Lists
Hello, the error is a null reference exception on the AddRange line. It seems to think that something there, either the List or one of the items being added, reference is null. The second script is one from one of the items being added. This is quite a simple issue but I can't pin down the cause. Can anyone offer advice or spot the probably obvious problem that I'm failing to see? I can offer more information if you should need to request it.
Thanks.
public void deathRelist(GameObject nullShip)
{
//find list with null ship and store
List<GameObject> listWithNullShip = FindListContainingShip(nullShip);
//create empty list for storage
List<GameObject> newListForShips = null;
//remove null ship from list
listWithNullShip.Remove(nullShip);
//store remaining ships in new lists
newListForShips.AddRange(listWithNullShip);
//clears and removes old list from master list
listWithNullShip.Clear();
listOfShipLists.Remove(listWithNullShip);
//relists all ships in new list one ship at a time.
foreach(GameObject ship in newListForShips)
{
ship.transform.parent.GetComponent<GridSquare>().reList();
}
newListForShips.Clear();
}
}
SECOND SCRIPT
public void Destroy()
{
if(canDestroy == true)
{
Debug.Log ("Destroy called");
//stores the ship which has been destroyed
nullShip = this.gameObject;
//resets parents reference variables
this.transform.parent.GetComponent<GridSquare>().currentShip = null;
this.transform.parent.GetComponent<GridSquare>().shipDisplay = null;
this.transform.parent.GetComponent<GridSquare>().currentShipType = null;
this.transform.parent.GetComponent<GridSquare>().isEmpty = true;
//deparents ship from grid
this.transform.parent = null;
//gets reference for arraholder
ArrayHolder scriptRef = reference1.GetComponent<ArrayHolder>();
//Calls method to relist the ships in null ships list
scriptRef.deathRelist(nullShip);
//Destroys this ship.
Destroy(this.gameObject);
}
}
}
public void shipPlaced(int x, int y, GameObject gridShip, string gridShipType)
{
List<GameObject> shipList;
bool adjacentShipFound = false;
List<GameObject> lastListAddedTo = null;
foreach(GameObject item in gridArray) //Look at every spot in grid
{
GridSquare currentSquare = item.GetComponent<GridSquare>();
int x2,y2;
//Read x2 y2 from item name
x2 = currentSquare.X;
y2 = currentSquare.Y;
//check if currentSquare next to newly placed ship
if(x2 == x+1 && y2 == y || x2 == x-1 && y2 == y || x2 == x && y2 == y+1 || x2 == x && y2 == y-1)
{
//AND if it contains a ship of the same type
if(currentSquare.currentShipType == gridShipType)
{
//identified a ship which is adjacent to our newly placed ship
GameObject adjacentShip = currentSquare.currentShip;
//Find the list this found ship exists in already
List<GameObject> matchingShipList = FindListContainingShip(adjacentShip);
//Have we already found a matching list?
if (adjacentShipFound == false)
{
//Add newlyAddedShip to the matchingShipList
matchingShipList.Add(gridShip);
lastListAddedTo = matchingShipList;
adjacentShipFound = true;
}
else
{
//Combine last list and matching list together
lastListAddedTo.AddRange(matchingShipList);
listOfShipLists.Remove(matchingShipList);
}
}
}
}
Answer by EvilWarren · Mar 03, 2014 at 04:11 PM
You are getting a null reference because your newListForShips list is in fact null. You set it with this line:
List<GameObject> newListForShips = null;
Change the line to this:
List<GameObject> newListForShips = new List<GameObject>();
This will instantiate your list so you can add to it.
Ah right, I was working under the impression that it would revert to not null when gameobject where added to it, thanks bro.
After applying your fix, I got some errors in another part of the script. This is called from the object with the second script, all the information being sent to it is correct and in order, and this works when an object is first passed through it, however, when it is passed through it via the deathRelist function (activated by the calling reList towards the bottom of the script, I get another null reference error when it tries to add at line 28. Any ideas or is there not enough information to analyse this properly? I added the code to the bottom of the question.
After this line:
List<GameObject> matchingShipList = FindListContainingShip(adjacentShip);
Put a debug statement for the contents of matchingShipList and report what you find.
Debug.Log(matchingShipList);
Also, it would help if you can list the code for the FindListContainingShip() method.
Your answer
Follow this Question
Related Questions
Add to generic list on start problem 1 Answer
A node in a childnode? 1 Answer
Search for an object by name null reference exception 1 Answer
NullReferenceException when adding String to list 1 Answer
List of Objects in Boo 0 Answers