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 chetanisinanand · Nov 16, 2014 at 10:41 PM · c#nullreferenceexceptionclasslistsinstances

Getting NullReferenceException when creating class instance using List

I'm trying to create instance of class using List instead of array and getting error , i don't know the exact cause

please let me know if there is any workaround to use List of Class objects Thanks in advance for any suggestion or code !

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
     
 [System.Serializable] public class BaseClass{
 public int index ;
 public  List<GameObject> itemList;
 }
 
 public class GameLogicClass : MonoBehaviour {
 
 public List<BaseClass> baseClassInstance;
 
 
     void Start(){
 
         for(int _m = 0;_m < 10;_m++){
 
                     baseClassInstance.Add(new BaseClass());
                     baseClassInstance[_m].index = 1;
 
         }
 
         Debug.Log(baseClassInstance[0].itemList.Count );  // < Getting ERROR here : NullReferenceException: Object reference not set to an instance of an object GameLogicClass.Start () 
 
     }
 }
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
0
Best Answer

Answer by Scribe · Nov 16, 2014 at 10:55 PM

I think you need to define what the list is of (though this may be optional as it sounds like you aren't getting an error here)

 public List<BaseClass> baseClassInstance;

and you need to initialise the list somwhere:

 void Start(){
     baseClassInstance = new List<BaseClass>();
     for(int _m = 0;_m < 10;_m++){
         baseClassInstance.Add(new BaseClass());
         baseClassInstance[_m].index = 1;
     }
     Debug.Log(baseClassInstance[0].itemList.Count );
 }


Also in future can you highlight all your code and hit the '101010' button or press 'Ctrl+K' to format your code!

Thanks,

Scribe

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 Scribe · Nov 16, 2014 at 10:56 PM 0
Share

Oh looks like someone beat me to doing the reformatting for you! :D

avatar image chetanisinanand · Nov 16, 2014 at 11:09 PM 0
Share

i just edited my post , could you please have a look , it went bad because of hitting login/post button before completing my question :-P

avatar image chetanisinanand · Nov 16, 2014 at 11:28 PM 0
Share

Thanks Scribe for your quick reply, I just tried your suggestion and initialised list inside Start(), however i'm still getting the same error Note: I'm able to print/overwrite "index" value , the only problem is with "itemList"

avatar image Scribe · Nov 16, 2014 at 11:50 PM 0
Share

You need to initialise that as well! On my phone so sorry for any bad formatting

 public  List<GameObject> itemList = new List<GameObject>();

Or by doing:

 baseClassInstance.Add(new BaseClass());
 baseClassInstance[_m].index = 1;
 baseClassInstance[_m].itemList = new List<GameObject>();

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

27 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

All Arrayinstances get changed -1 Answers

Clicking out and then in to the Unity Player while game is running will something throw me a Null Exception. 2 Answers

Does a new instance of a custom class return null? 2 Answers

Unable to delete object from a list using foreach loop 1 Answer

Multiple Cars not working 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