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 NewfieJoe · May 06, 2011 at 12:42 AM · arrayexceptionhashtable

What am I doing wrong with hashtable.CopyTo?

I'm getting an error with the save routine for one of my javascript classes and I'm at a loss to see what's wrong with it.

public var RawSettings = new Hashtable();

// it gets loaded elsewhere with 3 key-value pairs, // verified by debug output showing a count of 3

function Save(){ Debug.Log("GamePrefs_Save initiated.\n Count is: " + RawSettings.Count ); var PrefKeys: String[]; if( FileName == "" ) { FileName = "Preferences"; Debug.Log("GamePrefs.Save: No FileName specified, using Preferences.ini"); } var srSaveFile : StreamWriter = new StreamWriter ( Application.persistentDataPath + "/" + FileName + ".ini" ); RawSettings.Keys.CopyTo(PrefKeys,0); for(var x = 0; x < PrefKeys.length; x++){ srSaveFile.WriteLine( PrefKeys[x] + "=" + RawSettings[PrefKeys[x]] as String ); } srSaveFile.Close (); Debug.Log( "GamePrefs: Saved " + FileName + " as " + Application.persistentDataPath + "/" + FileName + ".ini" ); }

Error message:

ArgumentNullException: Argument cannot be null. Parameter name: array System.Collections.Hashtable+HashKeys.CopyTo (System.Array array, Int32 arrayIndex)

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
3
Best Answer

Answer by loramaru · May 06, 2011 at 02:40 AM

You've only declared PrefKeys to be an array variable; you still need to instantiate an actual array object before making the call.

PrefKeys = new String[RawSettings.Keys.Count];
RawSettings.Keys.CopyTo(PrefKeys,0);
Comment
Add comment · Show 4 · 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 NewfieJoe · May 06, 2011 at 06:45 AM 0
Share

That was part of it. Now there's a new error with a String[] array or even an Array[] I get the same error. "InvalidCastException: Cannot cast from source type to destination type. System.Array.SetValue (System.Object value, Int32 index)"

Thanks for that tip though. :)

avatar image NewfieJoe · May 07, 2011 at 03:41 AM 0
Share

Oh and I upvoted your answer since it solves part of the problem, just hoping someone knows how to get it working in case another member needs to use the same function. I'm about ready to mirror the hashtable in an array at load so I can skip the CopyTo function altogether, though that's a kludgy fix. I just don't have time to rewrite the whole class and replace all the hashtable functions with array equivalents.

avatar image loramaru · May 07, 2011 at 08:43 AM 0
Share

That sounds like you have a key in your hash table that isn't a string. Oh and Array[] wouldn't help as that would be an array or arrays. You could try Object[] which would be able to hold anything. However you probably should find where you are storing an unexpected value (non-String) in the hash table and adjust that.

avatar image NewfieJoe · May 08, 2011 at 02:56 PM 0
Share

Ah crap.. it was a typo in some copy-pasted lines that caused the second error. Your fix workes fully now. Thanks!

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

No one has followed this question yet.

Related Questions

Hashtable and ArrayList problem 0 Answers

ExecutionEngineException: SIGILL 0 Answers

"Array out of range" when cycling weapons. 1 Answer

int array overflow exception 1 Answer

A null value was found where an object instance was required. 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