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 Flamacore · Jan 01, 2016 at 04:12 PM · javascriptnullreferenceexceptionarraysfor-loopsplit

Split() into a fixed length array?

Ok here's what i'm pulling my hair out for;

 DidoID = decryptedData2[0].ToString().Split(";"[0]);

I have an array setup like this. What i've been trying to do is to specify a length for DidoID and fill in the empty space after the Split happens.

For example the Split() gives me 3 items, i need to add 5 more fields as a string ("empty") to make its length 8. Another example would be Split() giving me 6 items and me needing to add 2 fields this time to make it 8.

8 is good. 8 is life :) So anyways. Here's what i've tried:

 for (var xa: int = 0; xa < 8 - DidoID.Length; xa++){
 DidoID.Add("empty");
 }

this gives:

 NullReferenceException: Object reference not set to an instance of an object

I have some ideas about possible reasons. Maybe tt could be something about Split() creating a different type of array? Or could it be something i miss in the for loop?
Can any of you guys help me? :) Thank you in advance...

Comment
Add comment · Show 1
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 gjf · Dec 31, 2015 at 09:07 PM 0
Share

although it's not clear what you're really trying to do (or why), i believe that you're making this more difficult than it needs to be ;)

why not initialize the entire array before splitting the data so any entries not created by the split function will already be initialized? your only problem then will be if the number of strings from the split exceed the limit of your array...

finally, since this question isn't specific to unity, just generic c# code, you might get answers over at stack overflow or similar sites.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Jan 01, 2016 at 06:00 PM

Native arrays can't be resized. You can use Array.Resize but it will create a new array anc copy the elements over.

 // UnityScript
 var length = DidoID.Length;
 Array.Resize(DidoID, 8);
 for(var i = length; i < 8; i++)
     DidoID[i] = "empty"; // init the new elements with an empty string.

Note: Array.Resize takes a ref-parameter so it's able to replace the array you pass in. If you want to pack those steps into a method, that method also needs a ref parameter. I'm not sure if you can actually define ref-parameters in UnityScript. In C# it would look like this:

 //C#
 public static void ResizeStringArray(ref string[] aArray, int aSize, string aDefaultString)
 {
     var length = aArray.Length;
     Array.Resize(ref aArray, aSize);
     for (int i = length; i < aSize; i++)
         aArray[i] = aDefaultString;
 }

 //C#
 ResizeStringArray(ref DidoID, 8, "empty");
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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Breaking up a string every A,B,C,D and store value behind it 3 Answers

going through each item of a multi-dimensional array 1 Answer

Another NullReferenceException: question...(Java) 1 Answer

Null Reference issue Teleporting player in js 2 Answers

Populating a Vector2 array in Javascript 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