- Home /
Null Reference Exception even after check
Hey. I've run into a little problem today. I have a script to save and load my pokémon party to PlayerPrefs, but for some reason it isn't working. Whenever I try to save, I get:
NullReferenceException: Object reference not set to an instance of an object
Party.savePokemon () (at Assets/Scripts/Party.cs:21)
This is my code:
public Pokemon[] party = new Pokemon[6];
public InitPokemon pokeDB = new InitPokemon();
public void savePokemon(int savenumber){
for (int i = 0; i < party.Length; i++) {
if (party[i].ID != null) {
PlayerPrefs.SetInt("Player."+savenumber+".Pokemon.Party."+i+".ID", party[i].ID); //LINE 21!
PlayerPrefs.SetString("Player."+savenumber+".Pokemon.Party."+i+".Nickname", party[i].nickname);
PlayerPrefs.SetInt("Player."+savenumber+".Pokemon.Party."+i+".Attack", party[i].Attack);
PlayerPrefs.SetInt("Player."+savenumber+".Pokemon.Party."+i+".SpAttack", party[i].SpAttack);
PlayerPrefs.SetInt("Player."+savenumber+".Pokemon.Party."+i+".Defense", party[i].Defense);
PlayerPrefs.SetInt("Player."+savenumber+".Pokemon.Party."+i+".SpDefense", party[i].SpDefense);
PlayerPrefs.SetInt("Player."+savenumber+".Pokemon.Party."+i+".Speed", party[i].Speed);
PlayerPrefs.SetInt("Player."+savenumber+".Pokemon.Party."+i+".HP", party[i].HP);
PlayerPrefs.SetInt("Player."+savenumber+".Pokemon.Party."+i+".CurrentHP", party[i].currentHP);
PlayerPrefs.SetInt("Player."+savenumber+".Pokemon.Party."+i+".CurrentEXP", party[i].currentExp);
}
}
}
public void loadPokemon(int savenumber){
for (int i = 0; i < party.Length; i++) {
if(PlayerPrefs.HasKey("Player."+savenumber+".Pokemon.Party."+i+".ID")){
party[i] = pokeDB.pokemonArray[PlayerPrefs.GetInt("Player."+savenumber+".Pokemon.Party."+i+".ID")-1];
party[i].ID = PlayerPrefs.GetInt("Player."+savenumber+".Pokemon.Party."+i+".ID");
party[i].nickname = PlayerPrefs.GetString("Player."+savenumber+".Pokemon.Party."+i+".Nickname");
party[i].Attack = PlayerPrefs.GetInt("Player."+savenumber+".Pokemon.Party."+i+".Attack");
party[i].SpAttack = PlayerPrefs.GetInt("Player."+savenumber+".Pokemon.Party."+i+".SpAttack");
party[i].Defense = PlayerPrefs.GetInt("Player."+savenumber+".Pokemon.Party."+i+".Defense");
party[i].SpDefense = PlayerPrefs.GetInt("Player."+savenumber+".Pokemon.Party."+i+".SpDefense");
party[i].Speed = PlayerPrefs.GetInt("Player."+savenumber+".Pokemon.Party."+i+".Speed");
party[i].HP = PlayerPrefs.GetInt("Player."+savenumber+".Pokemon.Party."+i+".HP");
party[i].currentHP = PlayerPrefs.GetInt("Player."+savenumber+".Pokemon.Party."+i+".CurrentHP");
party[i].currentExp = PlayerPrefs.GetInt("Player."+savenumber+".Pokemon.Party."+i+".CurrentEXP");
}
}
}
Any idea what I have done wrong/how to fix this?
EDIT: The Null is probably pointing towards party[i].ID, because when I change it to set 1, the error start to point on line 22. However I have the party set up in the editor, + there's the check so I don't get why it isn't working.
seems like the fragment of code posted has a blank line 21 (as reported by the error message) - which line does it refer to?
The code is shortened of Update() and Start(). There is comment on the real 21. line. Here it would be line 7
If the error is in party[i].ID, that means that party[i] is null for that value of i. This either means that it hasn't been assigned, or that it has been destroyed.
Try a Debug.Log(party[i]) just before where the error occurs, that'll clue you in to what's happening.
if you haven't initialized each entry of party[]
then you will receive the null reference exception. in c# it's not enough just to new the array/list/etc.
public Pokemon[] party;
private void InitParty(int partySize)
{
party = new Pokemon[partySize];
for (var i = 0; i < partySize; i++)
{
party[i] = new Pokemon();
}
}
then add a call to InitParty()
somewhere in your Awake()
/Start()
/etc.
InitParty(6);
you might consider initializing the entries with default values too...
EDIT: just saw the OP's edit so not sure it's an initialization issue...
Answer by webik150 · Sep 28, 2014 at 10:09 AM
Um... Okay I'm just stupid. The check should have been if(party[i] != null)
Thanks Baste