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
1
Question by jdog98 · Feb 04, 2015 at 01:42 AM · c#androiderrornullreferenceexception

Unexplainable NullReferenceException

This is the code

 activeNumberOfUnits = System.Int32.Parse(editorUnits[activeClass]);
 System.Array.Resize<string>(ref editorQPerUnit, editorQPerUnit.Length - editorQPerUnit.Length);
 System.Array.Resize<string>(ref editorQPerUnit, editorQPerUnit.Length + activeNumberOfUnits);
 for(int i = 0; i < activeNumberOfUnits; i++){
     editorQPerUnit[i] = GUI.TextField (new Rect(Screen.width/activeNumberOfUnits*i, Screen.height/10, Screen.width/activeNumberOfUnits, Screen.height/20), editorQPerUnit[i], 2);
 }

This isn't the first existence of these lines in the script. The same coding (different variables) is used before and after this error. The error is on line 5. Ive been stuck on this error all day. I looked at everything at least 10 times. I have even tried renaming all the variables that it uses. editorQPreUnit is a string array. activeNumberOfUnits is an int.

Comment
Add comment · Show 7
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 Alanisaac · Feb 04, 2015 at 03:24 AM 0
Share

Not an expert on Unity's internals, but does the static GUI.TextField(...) method require string text to be non-null?

Different class here, but it feels like a pretty similar situation. If that is really the case, it's a shame that Unity's internals aren't perfor$$anonymous$$g the null check and throwing ArgumentNullExceptions ins$$anonymous$$d.

avatar image jdog98 · Feb 04, 2015 at 03:28 AM 0
Share

It requires a string to edit, and can only edit a string, so yes

avatar image Alanisaac · Feb 04, 2015 at 03:39 AM 0
Share

Right well that's probably your null reference. The default value for a string is null. When you perform the array resize operations, you're not setting anything to the new array cells. So you've got null strings in your array which you hit with the for loop.

 System.Array.Resize<string>(ref editorQPerUnit, editorQPerUnit.Length - editorQPerUnit.Length);

Is always equivalent to...

 System.Array.Resize<string>(ref editorQPerUnit, 0);

And the resize operation truncates the array. Now editorQPerUnit is an array of length 0, so when you call...

 System.Array.Resize<string>(ref editorQPerUnit, editorQPerUnit.Length + activeNumberOfUnits);

You're left with an array of size activeNumberOfUnits filled with null. Call the for loop on that and if GUI.TextField(...) won't take null strings, there's your problem. Did you mean to do something else with the array resizes?

avatar image Alanisaac · Feb 04, 2015 at 03:56 AM 2
Share

Yes, editorQPerUnit is not null. But I'm saying editorQPerUnit[i] is.

And you call GUI.TextField (new Rect(...), *editorQPerUnit[i]*, 2); ... which is GUI.TextField (new Rect(...), *null*, 2); for any i.

Try setting all elements of your array to string.Empty right after you resize it the second time ins$$anonymous$$d of null. Alternatively, just call GUI.TextField (new Rect(Screen.width/activeNumberOfUnits*i, Screen.height/10, Screen.width/activeNumberOfUnits, Screen.height/20), string.Empty, 2);

avatar image fafase · Feb 04, 2015 at 05:00 AM 1
Share

I don't really see the point of your two lines, you first set it to 0 and then to activeNumberOfUnit. :

  editorQPerUnit = null;
  editorQPerUnit = new string[activeNumberOfUnit];
  
Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by VOTRUBEC · Feb 04, 2015 at 08:33 AM

If the array editorQPerUnit was null, trying to find editorQPerUnit.Length would result in an exception.

But by resizing your array upwards, you're adding null entries into the array.

Try defining defaults for the additional editorQPerUnit items. For example:

 for (int i = 0; i < editorQPerUnit.Length; i++)
 {
     if (editorQPerUnit[i] == null)
     editorQPerUnit[i] = String.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
avatar image
0

Answer by karma0413 · Feb 04, 2015 at 05:23 AM

Newbie response:

It appears that one of those two variables are null. 1. activeNumberOfUnits or editorQPerUnit[]

Its really as simple as that. Try 1 line up print (active) and also editorQper.... If your error on the code line falls on that print then you know it's that variable that is giving you null.

Ideally, it would be best to work out the logic to make sure null doesnt happen in the first place. if your not sure what I mean you could put this above line 5:

 if (activeNumberOfUnits == null){print ("it was the unit thingie");}
 if (editorQPerUnit[i] == null){print ("it was your array");}
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 jdog98 · Feb 04, 2015 at 07:35 AM 0
Share

Is it possible for an int to be null? I think by default, its 0. Even if one of them were null, witch it was, I would have been able to see it in the inspector. Problem solved, read the comments

avatar image VOTRUBEC · Feb 04, 2015 at 08:33 AM 0
Share

An int is zero by default and can't be null. UNLESS you define it as int? activeNumberOfUnits; (a nullable variable). See the $$anonymous$$SDN page.

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

22 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

Related Questions

Make sphere shoot to Touch.position error 1 Answer

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

Unity Editor & iOS - Download MP4 to Streaming Assets Folder 0 Answers

How to check for null 2 Answers

I cannot understand where the Object reference is not set to an instance of an object? 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