1
\$\begingroup\$

I know this question has been asked before but my case is different(or i could not gather any kind of help from those)

I am facing "NullReferenceException: Object reference not set to an instance of an object" Error

in Class 1 I tried to make an array of a Class2 type and that class2 had two arrays of type "int"

but when i am filling up values in those arrays of Class2, by making object of Class2. it seems to be null.

well right now its not a problem but when i will use those value in other place it will create one. so i would really like to solve this right now.

PS:- At first I used to fill those value by inspector plane in unity. but now i want to fill them up by programatically at runtime

Class1 Code

public class LevelStructure : MonoBehaviour 
{
public static LevelStructure instance;

public int numberOfColumns=5;
public int numberOfRows=5;
internal ColumnStructure[] columnStructures;

int[] tilemap = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
int[] jellymap = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
void Awake()
{
    instance = this;
    //numberOfColumns = transform.GetChildCount();
    //columnStructures = transform.GetComponentsInChildren<ColumnStructure>();

    // I added the below code to put values at runtime programatically.
    // before that i used to put those from unity inspector plane.
    for (int i = 0; i<numberOfColumns; i++) 
    {

        ColumnStructure cs = new ColumnStructure();

        for(int j = 0; j<numberOfRows; j++)
        {

            if(cs==null)
            {
                Debug.Log("CS is null");

            }
            else
            {
                Debug.Log("CS is not null "+j);
                cs.itemAvailability[j] = (int)tilemap[i*numberOfColumns+j]; //This line is getting null reference exception
                cs.jelly[j] = (int)jellymap[i*numberOfColumns+j];

            }

        }
        columnStructures[i] = cs;

    }

}



}

Class2 Code :-

public class ColumnStructure 
{

public int[] itemAvailability;
public int[] jelly;

}

Console Log :-

CS is not null 0
UnityEngine.Debug:Log(Object)
LevelStructure:Awake() (at Assets/CM3/Scripts/Game Scripts/LevelStructure.cs:35)

NullReferenceException: Object reference not set to an instance of an object
LevelStructure.Awake () (at Assets/CM3/Scripts/Game Scripts/LevelStructure.cs:36)

ANSWER :-

Thanks to Petr Abdulin, I modified Class2 Code like this.

public class ColumnStructure 
{

public int[] itemAvailability = {1,1,1,1,1,1,1,1};
public int[] jelly = {1,1,1,1,1,1,1,1};
void Awake()
{

}
}
\$\endgroup\$

closed as off-topic by Anko, Josh Jul 30 '14 at 15:44

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions that aren't specific to game development are off-topic here, but can be asked on Stack Overflow. A good rule of thumb is to ask yourself "would a professional game developer give me a better/different/more specific answer to this question than other programmers?"" – Anko, Josh
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ cs isn't null, but cs.itemAvailability is. Looks like you never populated that array. \$\endgroup\$ – rutter Jul 30 '14 at 6:27
  • \$\begingroup\$ @rutter : doesn't this statement cs.itemAvailability[j] = (int)tilemap[i*numberOfColumns+j]; count as populating the array ? if not then how should i achieve that ? \$\endgroup\$ – Pawan Joshi Jul 30 '14 at 6:42
2
\$\begingroup\$

Well, from you code I see that in line:

cs.itemAvailability[j] = (int)tilemap[i*numberOfColumns+j];

cs.itemAvailability is definitely null. You declare an array variable but it's never initialized.

You need something like

cs.itemAvailability = new int[size_of_array];

if you want it to be an initialized array.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.