2

ive got this class it has one 2dArray and when im trying to fill it im getting the error NullReferenceException: Object reference not set to an instance of an object AdminGrid.FullNot(Int32 Row, Int32 Column, Int32 Full, System.String PieceName)

public class AdminGrid : MonoBehaviour {

    public int numRows;
    public int numColumns;
    private int[,] ArrayGrid;
    // Use this for initialization
    void Init() {
                numColumns = 6;
                numRows = 6;
        ArrayGrid = new int[numRows,numColumns];

        for(int y = 0;y < numRows;y++)
        {
            for(int x = 0;x < numColumns;x++)
            {
                ArrayGrid[y,x] = 0; 
            }
        }
    }

    public void FullNot(int Row,int Column,int Full,string PieceName)
    {   
        ArrayGrid[Row,Column] = 1;//Error is here
    }

    public int WhatsonGrid(int Row,int Col)
    {
        return ArrayGrid[Row,Col];
    }
}

Any idea why this is happening? as you can see my array is filled with 0s i thought that would fix this but it seems like not,also i made sure the values are inside the array meaning the max for columns and rows are 5,5

im using the engine Unity so Init //actually called start

it gets called when the game starts as a rule thats why im pretty sure it is called

6
  • 1
    Where you call FullNot method, please add that code Commented May 19, 2012 at 18:49
  • Did you call Init at any point? Commented May 19, 2012 at 18:49
  • Are you calling FullNot before calling Init? Commented May 19, 2012 at 18:50
  • @Makenshi - As others asked already - please post the code where you call FullNot and the few lines before (also where you call Init, if possible). Commented May 19, 2012 at 19:01
  • Sorry as i added im using the engine Unity so Init is called automatically so theres no doubt its being called or so i thought it seems it is just printing 2 values Commented May 19, 2012 at 19:11

2 Answers 2

1

ArrayGrid is a reference-type field, so will be null by default, so I conclude simply: you haven't called Init. So: call Init.

Sign up to request clarification or add additional context in comments.

3 Comments

init is called im pretty sure of it
@Makenshi where is it called? If I'd had been called, the array would be non-null. It is the not reference here; not really much doubt...
ok the error was Init was being called at the same time as another inits somehow and so it was filling the array with garbage sorry
1

Rename Init with AdminGrid and all will be fine )


public AdminGrid()
{
...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.