-3

I'm writing a project about game's character data.

And each character in the data document have four types, Lv1 and LvMAX, and HP, STR, VIT, INT, MEN.

I use top one code at the middle part and got NullReferenceException when I use it to get some data like:

int x = CD.Parameters.Basic.Awaked.Force.Lv1.STR;

Force will be null. But when I use buttom one at the middle part, Force won't be null.

What's the difference between that two?

Code below

public class ParamType
{  
    public ParamLv Mebius, Force, Aegis, Magius;
    string cost;
    DataRow[] Datas;
    List<int> ToMebius = new List<int>(), ToForce = new List<int>(), ToAegis = new List<int>(), ToMagius = new List<int>(); //HP, HP, STR, STR, VIT, VIT, INT, INT, MEN, MEN

    public ParamType(SData Data, bool awaked)
    {
        if (awaked)
        {
            Data.CharaID = CharaCOM.AwakedID(Data.CharaID);
        }
        Datas = DataCOM.Search(Data.CharaID, Data.DTs.Source, Data.TitleP.Start[(int)DataTitle.CharacterParams], Const.COL_CHARACTER_ID, Const.COL_CHARACTER_ID);
        cost = DataCOM.Search(Data.DTs.Source, Data.CharaID, Const.COL_COST, 0, Data.TitleP.Start[(int)DataTitle.CharacterParams], Const.COL_CHARACTER_ID_WITH_TYPE);

        List<int>[] SArray = { ToMebius, ToForce, ToAegis, ToMagius };

        for (int i = 0; i < Datas.Length; i++)
        {
            SArray[i] = new List<int>();
            for (int j = Const.COL_PARAM_MIN; j < Const.COL_PARAM_MIN + Const.COL_PARAM_LENGTH; j++)
            {
                SArray[i].Add(Convert.ToInt32(Datas[i][j]));
            }
        }

        /*
        this will send NullReference Exception

        ParamLv[] PLArray = new ParamLv[4];

        for (int i = 0; i < SArray.Length; i++)
        {
            PLArray[i] = new ParamLv(Data, SArray[i]);
        }
        */

        /*
        This won't get exception and I can get correct data I want.

        Mebius = new ParamLv(Data, SArray[0]);
        Force = new ParamLv(Data, SArray[1]);
        Aegis = new ParamLv(Data, SArray[2]);
        Magius = new ParamLv(Data, SArray[3]);
        */
    }

    public class ParamLv
    {
        public Params Lv1, LvMax;
        List<int> ToLv1 = new List<int>(), ToLvMAX = new List<int>(); //HP, STR, VIT, INT, MEN

        public ParamLv(SData Data, List<int> ParamsL)
        {

            for (int i = 0; i < ParamsL.Count; i += Const.COL_PARAM_MIN_MAX_GAP)
            {
                ToLv1.Add(ParamsL[i]);
                ToLvMAX.Add(ParamsL[i + 1]);
            }
            Lv1 = new Params(Data, ToLv1);
            LvMax = new Params(Data, ToLvMAX);
        }

        public class Params
        {
            //some method and properties to get or set Parameters. 
        }
    }

Please tell me if something still bad, and this is my first time to ask question here, so If I did something wrong, please tell me. Thanks for @MicroVirus , @Moriarty and @mvikhona told my mistake.

pfnuesel
  • 14,093
  • 14
  • 58
  • 71
  • 2
    Please provide a [[mcve]]; it's really unclear what your code is and what goes wrong. – MicroVirus Apr 02 '16 at 13:18
  • As @MicroVirus said, please provide a complete sample. Which means at least to also add the definitions of ParamLv, SArray and Data. – Juliën Apr 02 '16 at 13:21
  • 1
    Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Rob Apr 02 '16 at 13:24
  • @RaenonX Please mention what is 'Force' and kindly re-frame your question – mvikhona Apr 02 '16 at 13:26
  • Please forgive me that I didn't post the code clearly, and I edited already, Thanks you guys – RaenonX JELLYCAT Apr 03 '16 at 02:12

1 Answers1

0
Mebius = new ParamLv(Data, SArray[0]);
Force = new ParamLv(Data, SArray[1]);
Aegis = new ParamLv(Data, SArray[2]);
Magius = new ParamLv(Data, SArray[3]);

This works, because you are assigning reference to new ParamLv to your properties.

But in this case:

ParamLv[] PLArray = { Mebius, Force, Aegis, Magius };

for (int i = 0; i < PLArray.Length; i++)
{
     PLArray[i] = new ParamLv(Data, SArray[i]);
}

you aren't filling your array with variables/properties themselves, but you are filling it with references your properties hold, in the end your array will hold reference to 4 new ParamLw, but your property Force will stay null.

Edit: I'll try to explain it a bit different. Let's say you have this code:

ParamLv[] PLArray = { Force };

At this moment value of PLArray[0] is same as value of Force, but PLArray[0] isn't Force. The moment you do this:

PLArray[0] = new ParamLv(Data, null);

new ParamLv(Data, null) returns reference to new ParamLv and you assign this to your PLArray[0], but like I said before PLArray[0] isn't Force, so Force will stay unchanged.

If that didn't explain it well, try to look at this piece of code, it does what you are trying to do.

int a = 1;
int[] myArr = { a }; // a = 1, myArr[0] = 1
myArr[0] = 2; // a = 1, myArr[0] = 2

object obj = null;
object[] objArr = { obj }; // obj = null, objArr[0] = null
objArr[0] = new object(); // obj = null, objArr[0] = 'reference to new object'
CrudaLilium
  • 654
  • 7
  • 18
  • so you mean that I created an object but it is null when I use bottom one expression? – RaenonX JELLYCAT Apr 03 '16 at 02:13
  • but SArray isn't empty.I set a interrupt point at the end, both expression will have some Properties in ParamLv, but when the code run to main program, object "Force" will be null – RaenonX JELLYCAT Apr 03 '16 at 02:16
  • I edited my answer in attempt to clarify. I looked at your code and there is similar problem with your SArray, in your for loop you create new instance of list and again assign it to your SArray[index], your ToMebius etc. won't be null, but they will stay empty. – CrudaLilium Apr 03 '16 at 11:43