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

Discussion in 'Windows - General discussion' started by baba_944, Jul 14, 2014.

  1. baba_944

    baba_944 Newbie

    Joined:
    Jul 13, 2014
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    1
    I don't know if I'm legally suppose to post this in "AfterDawns" or not. I've accidently ticked of people due to my inexperience with programming (C#, which code I'm posting now is my first language). Here's a topic for example which seems to be no help (I've learned what arrays are in Mr. Adamson's video): http://www.reddit.com/r/learnprogramming/comments/2aom1z/back/

    Once again my apologies of this is here illegally. Thanks.

    Code:
    using System.Collections;
    
    public class Loops : MonoBehaviour {
    
       public class sonicHeroes {
    
       public int id; 
       public string knuckles;
       public string omega;
       public string big;
    
    
    
       public void powerCharacters() {
    
       print("Knuckles " + knuckles);
       print("Omega " + omega);
       print("Big " + big);
    
    
       }
    
       }
    
       void Start () {
    
    
       sonicHeroes[] powerCharac = new sonicHeroes[4];
    
       for (int i = 0; i <= 4; i++)
    
         {
           powerCharac[i] = new sonicHeroes();
           powerCharac[i].knuckles = "Knuckles";
           powerCharac[i].omega = "Omega";
           powerCharac[i].big = "Big";
        
        
         }
         for (int i = 0; i <= 4; i++)
         {
           powerCharac[i].powerCharacters();
      
        
         }
    
     
    Last edited by a moderator: Jul 15, 2014
  2. ps355528

    ps355528 Active member

    Joined:
    Aug 17, 2010
    Messages:
    1,071
    Likes Received:
    28
    Trophy Points:
    78
    "Object reference not set to an instance of an object"

    Correct ..
     
  3. Ketola

    Ketola Turned ninja Staff Member

    Joined:
    Jun 10, 1999
    Messages:
    1,233
    Likes Received:
    102
    Trophy Points:
    78
    First of all I wrapped your code in CODE-tags to make it more clear (otherwise [i] becomes italics).

    Second of all, it's been quite a while since I last touched C, and you seem to have gotten pretty good responses over at reddit already. As mentioned there, in an array of n elements the index of the last element is n-1. Instead of looping to a fixed count (4) you could just loop to powerCharac.Length-1. E.g.
    Code:
    for (int i = 0 ; int <= powerCharac.Length-1 ; i++ ) {
    ....
    }
     
  4. baba_944

    baba_944 Newbie

    Joined:
    Jul 13, 2014
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    1
    IS this for both loops or for the second one?
     
  5. Ketola

    Ketola Turned ninja Staff Member

    Joined:
    Jun 10, 1999
    Messages:
    1,233
    Likes Received:
    102
    Trophy Points:
    78
    For both of them. There could be something else that needs fixing as well, but at least the element index is an obvious problem.
     
  6. baba_944

    baba_944 Newbie

    Joined:
    Jul 13, 2014
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    1
    Thanks. I'm going on switch statements and I found this error which shows the red line by my method name getSonic () and break;
    Code:
    using UnityEngine;
    using System.Collections;
    
    public class RandomNumbers : MonoBehaviour {
    
        public class sonicTeams {
      
           
        public string teamSonic;
        public string teamDark;
        public string teamRose;
        public string teamChaotix;
        public int age;
        public int health;   
          
        public void teamInfo () {
      
        print("Team Sonic " + teamSonic);
        print("Team Dark " + teamDark);
        print("Team Rose " + teamRose);
        print("Team Chaotix " + teamChaotix);
      
            }
          
        }
      
        void Start () {
      
        float teamAge;
        float teamNumber;
        float teamSize;
        float teamHealth;
          
        sonicTeams[] sonicTeam = new sonicTeams[51];
      
            for (int i = 1; i <= 50; i++)
          
            {
                teamAge = Random.value;
                teamSize = Random.value;
              
                teamNumber = Mathf.RoundToInt(12 * teamAge + 4);
                teamHealth = Mathf.RoundToInt(5 * teamSize + 6);
              
              
                sonicTeam[i] = new sonicTeams();           
                sonicTeam[i].teamSonic = getSonic();
                sonicTeam[i].teamDark = getDark();
                sonicTeam[i].teamRose = getRose();
                sonicTeam[i].teamChaotix = getChaotix();
                sonicTeam[i].age = (int)teamNumber;
                sonicTeam[i].health = (int)teamHealth;
              
                }
            for (int i = 1; i <= 10; i++)
              
            {
                sonicTeam[i].teamInfo();
              
            }  
          
            string getSonic ()
              
            string tName;
            float rNum;
            int mySlot;  
          
            rNum = Random.value;
            mySelection = (int)Mathf.RoundToInt(10 * rNum + 4);  
          
            switch(mySelection) {
                case 1: tName = "Sonic"
                break;
                case 2: tName = "Tails";   
                  
            }
        }
    }
    
    EDIT: Error codes are the following: Assets/RandomNumbers.cs(73,29): error CS1525: Unexpected symbol `break' and Assets/RandomNumbers.cs(62,32): error CS1525: Unexpected symbol `(', expecting `)', `,', `;', `[', or `='
     
  7. baba_944

    baba_944 Newbie

    Joined:
    Jul 13, 2014
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    1
    I fixed the break issue.
     
  8. Ketola

    Ketola Turned ninja Staff Member

    Joined:
    Jun 10, 1999
    Messages:
    1,233
    Likes Received:
    102
    Trophy Points:
    78
    Good to hear. The Switch seemed a bit broken. Feel free to post your current code & progress. =)
     
  9. baba_944

    baba_944 Newbie

    Joined:
    Jul 13, 2014
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    1
    May you tell me what I did wrong for this: string getSonic () { with this error code: Assets/RandomNumbers.cs(62,32): error CS1525: Unexpected symbol `(', expecting `)', `,', `;', `[', or `='

    Code:
    using UnityEngine;
    using System.Collections;
    
    public class RandomNumbers : MonoBehaviour {
    
        public class sonicTeams {
       
            
        public string teamSonic;
        public string teamDark;
        public string teamRose;
        public string teamChaotix;
        public int age;
        public int health;    
           
        public void teamInfo () {
       
        print("Team Sonic " + teamSonic);
        print("Team Dark " + teamDark);
        print("Team Rose " + teamRose);
        print("Team Chaotix " + teamChaotix);
       
            }
           
        }
       
        void Start () {
       
        float teamAge;
        float teamNumber;
        float teamSize;
        float teamHealth;
           
        sonicTeams[] sonicTeam = new sonicTeams[51];
       
            for (int i = 1; i <= 50; i++)
           
            {
                teamAge = Random.value;
                teamSize = Random.value;
               
                teamNumber = Mathf.RoundToInt(12 * teamAge + 4);
                teamHealth = Mathf.RoundToInt(5 * teamSize + 6);
               
               
                sonicTeam[i] = new sonicTeams();            
                sonicTeam[i].teamSonic = getSonic();
                sonicTeam[i].teamDark = getDark();
                sonicTeam[i].teamRose = getRose();
                sonicTeam[i].teamChaotix = getChaotix();
                sonicTeam[i].age = (int)teamNumber;
                sonicTeam[i].health = (int)teamHealth;
               
                }
            for (int i = 1; i <= 10; i++)
               
            {
                sonicTeam[i].teamInfo();
               
            }   
           
            string getSonic () {
               
           
            string tName;
            float rNum;
            int mySlot;   
           
            rNum = Random.value;
            mySelection = (int)Mathf.RoundToInt(2 * rNum + 1);   
           
            switch(mySelection) {
                case 1: tName = "Sonic";
                break;
                case 2: tName = "Tails";    
                break;
                case 3: tName = "Knuckles";
                break;       
                   
            }
            }
       
        }
    }
    
     
  10. Ketola

    Ketola Turned ninja Staff Member

    Joined:
    Jun 10, 1999
    Messages:
    1,233
    Likes Received:
    102
    Trophy Points:
    78
    I would guess that your string getSonic() method would have to return a string. Beyond that, I would really recommend going to the basics, and learning to interpret the errors thrown by the compiler.
     
  11. baba_944

    baba_944 Newbie

    Joined:
    Jul 13, 2014
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    1
    Back. I've seen this done in a video with no while loops (I don't know what those are yet) no anything except what I have posted. Mine's an almost exact replica of his code. What I'm trying to do is to run my switch statement in Unity's console. Here's my code:
    Code:
    public class teams {
           
            public string teamDark;
            public string teamSonic;
            public int teamRose;
            public int teamChaotix;
           
            public void teamData () {
           
            print("Team Dark " + teamDark);
            print("Team Sonic " + teamSonic);
            print("Team Rose " + teamRose);    
            print("Team Chaotix " + teamChaotix);
               
            }
           
        }
    
        void Start () {
       
        float teamNumber;    
        float teamSize;
        float teamPosition;
        float teamGin;
           
        teams[] teamInfo = new teams[51];
       
        for(int i = 1; i<= 50; i++)
           
            {   
           
            teamNumber = Random.value;
            teamPosition = Random.value;
               
            teamSize = (int)Mathf.RoundToInt(150 * teamNumber + 12);
            teamGin = (int)Mathf.RoundToInt(500 * teamPosition + 1);
           
            teamInfo[i] = new teams();
               
            teamInfo[i].teamDark = getDark();
            teamInfo[i].teamSonic = getSonic();
            teamInfo[i].teamRose = (int)teamSize;
            teamInfo[i].teamChaotix = (int)teamGin;
           
            }
            for(int  i = 1; i<= 10; i++)
           
            {
                teamInfo[i].teamData();
            }
        }
       
        string getDark () {
           
        string mRouge;
        float rShadow;
        int mySelection;
       
        rShadow = Random.value;
        mySelection = (int)Mathf.RoundToInt(6 * rShadow + 1);
       
        switch(mySelection){
       
           
            case 1: mRouge = "Rouge";
            break;
            case 2: mRouge = "Shadow";   
            break;
            case 3: mRouge = "Omega";
            break;    
            case 4: mRouge = "Team Dark";
            break;
            case 5: mRouge = "This Machine";
            break;
            default: mRouge = "Sonic Heroes";
            break;
           
            return mRouge;    
               
            }
        }
    }
    
    Here's the errors/warning:

    1: error CS0161: `Switch.getDark()': not all code paths return a value
    2: warning CS0162: Unreachable code detected
    3: I know what to do with error number 3. But still tosses my error #1 error.
     
  12. Ketola

    Ketola Turned ninja Staff Member

    Joined:
    Jun 10, 1999
    Messages:
    1,233
    Likes Received:
    102
    Trophy Points:
    78
    At least the return mRouge; should be outside the switch statement for the getDark() method to return anything. That could also be the cause behind the 2nd warning, as the return cannot be reached.
     
  13. baba_944

    baba_944 Newbie

    Joined:
    Jul 13, 2014
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    1
    Thank you it works! That was a simple fix that I never really catch myself. Thank you.
     
  14. Ketola

    Ketola Turned ninja Staff Member

    Joined:
    Jun 10, 1999
    Messages:
    1,233
    Likes Received:
    102
    Trophy Points:
    78
    Good to hear. =)
     

Share This Page