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 JerryCic · Jan 02, 2012 at 06:32 PM · c#globalvariables

null refference exception. right after i dimention the object array??

Hi

The code looks good. Its pretty, its symetrical. It's suppose to work. but it doesn't.

I get a null reference at the line indicated. How can i create an empty structure using nested classes?

 using UnityEngine;
 using System.Collections;

 public class Song : MonoBehaviour {
 // Home for the Main Data structure
 // Is an Array of Measures
 // Each Measure is an array of Chords
 // Each Chord is an array of Notes
 // Each Note is an Array of Game Objects
 private class Measures
 {
     public class Chords
     {
         public class Notes
         {
             public GameObject[] Sound;
         }
         public Notes[] Note;
     }
     public Chords[] Chord; 
 }

 public int MaxMeasures;
 public int ChordsPerMeasure;
 public int NotesPerChord;
 public int MaxObjectsPerNote;

 Measures[] Measure;
 
 void Start()
 {
     Debug.Log(MaxMeasures); // returns 1
     Measure = new Measures[MaxMeasures+1];
     Debug.Log(Measure.GetUpperBound(0)); // returns 1
     int M = 0;
     for (M = 1; M <= MaxMeasures; M++)
     {
         // next two lines generates null refference error
         Debug.Log(Measure[M].ToString());
         Measure[M].Chord = new Measures.Chords[ChordsPerMeasure+1];
         int C = 0;
         for (C = 1; C <= ChordsPerMeasure; C++)
         {
             Measure[M].Chord[C].Note = new Measures.Chords.Notes[NotesPerChord + 1];
             int N = 0;
             for (N = 1; N <= NotesPerChord; N++)
             {
                 Measure[M].Chord[C].Note[N].Sound = new GameObject[MaxObjectsPerNote + 1];
                 int S = 0;
                 for (S = 1; S <= MaxObjectsPerNote; S++)
                 {
                     Measure[M].Chord[C].Note[N].Sound[S] = null;
                 }

             }
         }
     }
 }

}

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

Answer by Aleron · Jan 02, 2012 at 07:14 PM

The null reference exception is because even though you are creating the array, you do not seem to be putting any Measure objects into it. Simply creating the array does not fill it with valid, non-null objects. So you will want to populate the array with Measure objects.

Also, you are setting M to 1 in your for loop, but the first index into the array should be index 0 (which you actually set M to in the line just before the for loop). If you set M to 0 in the for loop then it should process the loop without overrunning the end of the array. You will also run into the same issue with your for loop for C, with the same changes fixing that one as well.

Finally, you are creating the array with a size of maxMeasures + 1, which means your array will hold 1 more Measure object than you are saying you have. You may want to change the array allocation statement to just create an array the size of maxMeasures and then change your for loop to test M < maxMeasures rather than M

If you're new to programming, getting used to 0 based arrays can take a little time, but overall it's not too difficult once you get used to starting with 0 instead of 1 for counting things.

I hope that helps. :)

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 JerryCic · Jan 02, 2012 at 07:50 PM

Hi Thank you for taking the time to read my code. Here is the final script (so far) and it is working

 using UnityEngine;
 using System.Collections;

 public class Song : MonoBehaviour {
 // Home for the Main Data structure
 // Is an Array of Measures
 // Each Measure is an array of Chords
 // Each Chord is an array of Notes
 // Each Note is an Array of Game Objects
 public class Notes
 {
     public GameObject[] Sound;
 }
 public class Chords
 {
     public Notes[] Note;
 }
 public class Measures
 {
     public Chords[] Chord;
 }


 public int MaxMeasures;
 public int ChordsPerMeasure;
 public int NotesPerChord;
 public int MaxObjectsPerNote;

 Measures[] Measure;
 
 void Start()
 {
     Measures[] NewMeasures = new Measures[MaxMeasures];
     Measure = NewMeasures;
     int M = 0;
     for (M = 0; M < MaxMeasures; M++)
     {
         Measure[M] = new Measures();
         Measure[M].Chord = new Chords[ChordsPerMeasure];
         int C = 0;
         for (C = 0; C < ChordsPerMeasure; C++)
         {
             Measure[M].Chord[C] = new Chords();
             Measure[M].Chord[C].Note = new Notes[NotesPerChord];
             int N = 0;
             for (N = 0; N < NotesPerChord; N++)
             {
                 Measure[M].Chord[C].Note[N] = new Notes();
                 Measure[M].Chord[C].Note[N].Sound = new GameObject[MaxObjectsPerNote];
                 int S = 0;
                 for (S = 0; S < MaxObjectsPerNote; S++)
                 {
                     Measure[M].Chord[C].Note[N].Sound[S] = null;
                 }
             }
         }
     }
 }

}

Comment
Add comment · Show 1 · 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 Aleron · Jan 02, 2012 at 08:04 PM 0
Share

You're welcome and I'm glad I could help. Ins$$anonymous$$d of posting an answer with your modified code, you may want to edit your original question and post the modified code as well as add a note that you edited it to make the changes from the answers below it. :)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do i create a global structure and how do i acces it from any script? 3 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

C# GUI.Tooltip If Statement 2 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