-1

I'm trying to pass an array that comes from the Base class in the constructor of the Child class, however I feel like there might be conflict in terms of constructors getting called as the getFullArray() method of the Base class returns null.

Base class looks like this:

    protected Byte[,] _byteTileArray; //is going to be implemented in child class
    private Object[,] objectTileArray;

    public Level_Manager(ContentManager content)
    {
        CreateTileArray();

        objectTileArray = new Object[_byteTileArray.GetLength(0), _byteTileArray.GetLength(1)];

        CreateWorld(content);
    }

    protected abstract void CreateTileArray();

    private void CreateWorld(ContentManager content)
    {
        for(int row = 0; row < _byteTileArray.GetLength(0); row++)
        {
            for (int col = 0; col < _byteTileArray.GetLength(1); col++)
            {
                switch (_byteTileArray[row,col]) 
                {
                    case 0: objectTileArray[row, col] = null; break;
                    case 1: objectTileArray[row, col] = new Cube(new Vector2(col * (852/12), row * (480/7)), content, 124, 33, new Vector2(0, 0)); break;
                    case 2: objectTileArray[row, col] = new Ladder(new Vector2(col * (852 / 12), row * (480 / 7)-33), content, 50, 107, new Vector2(0, 0)); break;
                }
            }
        }
    }

    public Object[,] getFullArray(){

        return objectTileArray;
   }

I need the objectTileArray from getFullArray()

This is the Child class constructor:

private Object[,] arrayObjects;
    #endregion

    public Level1(ContentManager content, GraphicsDevice device) : base(content)
    {
        this.content = content;

        arrayObjects = getFullArray();

        hero = new Hero(new Vector2(1, device.Viewport.Height-40), this.content, arrayObjects);
    }

arrayObjects is null here.

Axelle
  • 442
  • 1
  • 9
  • 24
  • 1
    Have you done some debugging? – Maarten Jan 19 '17 at 11:45
  • 2
    Well there's nothing obvious in the code. Step through and see whats what. – Tim Rutter Jan 19 '17 at 11:47
  • 1
    Temporarily convert the fields into a property, and set a breakpoint in the setter. See what happens. – Maarten Jan 19 '17 at 11:47
  • I can´t see anything in the code causing `objectTileArray` within the base-class to be `null`. Maybe some multuithreading involved? – MakePeaceGreatAgain Jan 19 '17 at 11:48
  • @Maarten I've put breakpoints to check the values and it's null when the child constructor gets called but I don't know how else I can debug it :( – Axelle Jan 19 '17 at 11:48
  • @Axelle Step through it then, step-by-step. – Maarten Jan 19 '17 at 11:49
  • 2
    @Axelle: The obvious thing you can do is put a breakpoint at the point you expect it to be created and see if a) it gets created and b) it is called before the code that is trying to read it. – Chris Jan 19 '17 at 11:51
  • @HimBromBeere: I misread the question at first (in part because the OP talked about order of constructors). I had in fact just retracted the close vote as you commented. ;-) – Chris Jan 19 '17 at 11:52
  • 1
    Anyway you should debug step by step, meaning from base-class constructor where you set `objectTileArray` and than line by line. – MakePeaceGreatAgain Jan 19 '17 at 11:52
  • Btw.: you wrote "`_byteTileArray; //is going to be implemented in child class`". I guess it isn´t yet initialzed and thus your array stays empty (not null). Where do you set its actual value? – MakePeaceGreatAgain Jan 19 '17 at 11:56
  • 1
    I suspect this is caused by http://stackoverflow.com/questions/119506/virtual-member-call-in-a-constructor . You are calling a virtual method in the base class and if that method in the child class assumes the constructor of the child class has already been run that it will not work. For example if the `CreateTileArray` implementation relies on `this.Content` being set in the constructor then it will not work properly because that constructor has not yet been run. What you are doing is, in essence, dangerous. If you can show us `CreateTileArray` code then we can confirm this. – Chris Jan 19 '17 at 11:56
  • @Chris I also suppsed this, but from the OPs code we can´t assume this, there is no virtual member, but maybe OP forgot to mention making this question not answerable because of missing information. – MakePeaceGreatAgain Jan 19 '17 at 11:59
  • @HimBromBeere: `CreateTileArray` is abstract and called by the base constructor and I assume this is what sets up `_byteTileArray`. – Chris Jan 19 '17 at 12:01
  • @Chris `abstract`, not `virtual`. Anyway after the call to that method the base-class constructor sets `objectTileArray` which is used in the chield-class constructor. However this is everything just a guess, we need more info from OP. This is why I´m voting to close that question. – MakePeaceGreatAgain Jan 19 '17 at 12:03
  • @HimBromBeere: abstract or virtual will have the same practical effect. You can run something in a child class that may assume that its constructor has been run when it won't have been. – Chris Jan 19 '17 at 12:09
  • 1
    I managed to find it by actually debugging it better! updated it. Thank you guys so much :) – Axelle Jan 19 '17 at 12:12

1 Answers1

0

EDIT: Apparently I misunderstood the debugging and my arrayObjects wasn't null but the elements inside were.

    protected override void CreateTileArray()
    {
        base._byteTileArray = new Byte[,]
        {
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0},
            {0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0},
            {1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0},
            {0, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1},
            {1, 1, 0, 2, 1, 0, 1, 1, 1, 1, 1, 2},
            {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
        };
    }

this was the byteTileArray I was passing on to the Parent class, it was inserting null on the 0's (intentionally) but this was giving me a NullReferenceException in another part which was fixed by just adding if not null clause. Thank you to everyone and sorry!

Axelle
  • 442
  • 1
  • 9
  • 24