Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Jarlarko · Mar 03, 2014 at 03:48 PM · listnullreferenceexceptionadd

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);
                         
                     }                        
                 }
             }
         }    
         
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 3 · Share
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
avatar image Jarlarko · Mar 03, 2014 at 05:25 PM 0
Share

Ah right, I was working under the impression that it would revert to not null when gameobject where added to it, thanks bro.

avatar image Jarlarko · Mar 03, 2014 at 05:44 PM 0
Share

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.

avatar image EvilWarren · Mar 04, 2014 at 12:10 PM 0
Share

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

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

21 People are following this question.

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

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


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