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 Omega3939 · Nov 11, 2017 at 03:14 AM · nullreferenceexceptionarrayslists

Array of Lists null exception when adding to list.

I have made absolutely certain that none of the values used in this code where I'm getting the error are null. I've used breakpoints, I've used Debug.Log() I can not find the cause of this error for the life of me.

Full error text:

 NullReferenceException: Object reference not set to an instance of an object
 MapBuilder.generateStars (Int32 x) (at Assets/MapBuilder.cs:62)
 MapBuilder.UniverseInit () (at Assets/MapBuilder.cs:38)
 MapBuilder.Start () (at Assets/MapBuilder.cs:31)

Code: https://hastebin.com/yozozukufu.cs

A little explanation: I've got a data sheet with 45000 stars. I don't want to instantiate them all at once and thus render them all at once so I'm trying to divide the stars into chunks that can be loaded and unloaded as needed. To assign a star to a chunk I'm making an array of lists sectorsList[,,] (line 20, 30) the array is 3 dimensional because I can assign a star to a chunk by taking its position and flooring it to an int and then making that the location. so that for example stars with locations 2.6, 1.2, 3.1 and 2.1, 1.0, 3.8 would both be found in the list [2, 1, 3] however when I actually try to add the stardata to the list at the location that i've calculated (line 62) it gives me a null reference exception despite stardata not being null or any other value that it uses being null. I have confirmed this with the debugger. and with Debug.Log();

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by jchester07 · Nov 11, 2017 at 06:09 AM

I'm not really good at Arrays specially on multi dimension arrays. You said that you have 45,000 stars, but in line 36 you are clamping from 1 to 45,099. Should it be 1 to 45,000?

Comment
Add comment · 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
0

Answer by Bunny83 · Nov 11, 2017 at 10:13 AM

You created your multidimensional array but every element in that array has it's default value "null". You haven't created a single List<StarData>. So of course you don't have a single List where you can add something to.

You may want to do something like this:

 var sector = sectorsList[h, i, j];
 if (sector == null)
     sector = sectorsList[h, i, j] = new List<StarData>();
 sector.Add(stardata);


May I ask how large "maxSecDist" is? Keep in mind that you create a multidimensional array of rank 3. So the number of elements grow with the cubic function. If it's "10" you do 20*20*20 == 8000 elements. If it's "18" you have 36*36*36 == 46656 elements. If it's "200" you would have 8 million elements where each element can hold a List<StarData> but is initially null as mentioned. Also do you actually want to add multiple stars to the same sector? Currently you use the same coordinates for the star position and the sector position. That would mean you can only have one star per sector or they actually have the same position within the system.

Comment
Add comment · Show 2 · 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 pako · Nov 11, 2017 at 10:25 AM 0
Share

Ooops, I was writing up a lengthy answer without noticing that @Bunny83 had already posted an answer... sorry...

avatar image Bunny83 pako · Nov 11, 2017 at 11:32 AM 0
Share

No worries... It happens to me countless times. Since i also write rather "long" answers at the time i actually submit my answer there might already be others.

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

75 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 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 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 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

Null Ref Exception on a script that should have a reference in it? 2 Answers

MergeSort function acting strange 0 Answers

Custom Inspector: For every new element in one array create a brand new array? 0 Answers

Inventory String error 1 Answer

How can I create a tree like list/array structure? 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