- Home /
Question by
Fatal.strings · Sep 05, 2015 at 03:18 PM ·
c#serializationnull reference exception
Null Reference Exception on a static array (Deserialize) - what am I doing wrong
Its giving me a null reference error even though I'm using a static array variable
any help is greatly appreciated
heres the entire Script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public static class SaveFileHandler {
public static SaveValues[] SaveGames;
public static bool File1, File2, File3, File4, File5, File6, File7, File8, File9, File10, FileA;
public static void CheckFiles(){
if(File.Exists(Application.dataPath + "/SaveFiles/Slot I.Save")){
File1 = true;
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.dataPath + "/SaveFiles/Slot I.Save", FileMode.Open);
SaveFileHandler.SaveGames[1] = (SaveValues)bf.Deserialize(file);
file.Close();
}
if(File.Exists(Application.dataPath + "/SaveFiles/Slot II.Save")){
File2 = true;
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.dataPath + "/SaveFiles/Slot II.Save", FileMode.Open);
SaveFileHandler.SaveGames[2] = (SaveValues)bf.Deserialize(file);
file.Close();
}
if(File.Exists(Application.dataPath + "/SaveFiles/Slot III.Save")){
File3 = true;
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.dataPath + "/SaveFiles/Slot III.Save", FileMode.Open);
SaveFileHandler.SaveGames[3] = (SaveValues)bf.Deserialize(file);
file.Close();
}
if(File.Exists(Application.dataPath + "/SaveFiles/Slot IV.Save")){
File4 = true;
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.dataPath + "/SaveFiles/Slot IV.Save", FileMode.Open);
SaveFileHandler.SaveGames[4] = (SaveValues)bf.Deserialize(file);
file.Close();
}
if(File.Exists(Application.dataPath + "/SaveFiles/Slot V.Save")){
File5 = true;
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.dataPath + "/SaveFiles/Slot V.Save", FileMode.Open);
SaveFileHandler.SaveGames[5] = (SaveValues)bf.Deserialize(file);
file.Close();
}
if(File.Exists(Application.dataPath + "/SaveFiles/Slot VI.Save")){
File6 = true;
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.dataPath + "/SaveFiles/Slot VI.Save", FileMode.Open);
SaveFileHandler.SaveGames[6] = (SaveValues)bf.Deserialize(file);
file.Close();
}
if(File.Exists(Application.dataPath + "/SaveFiles/Slot VII.Save")){
File7 = true;
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.dataPath + "/SaveFiles/Slot VII.Save", FileMode.Open);
SaveFileHandler.SaveGames[7] = (SaveValues)bf.Deserialize(file);
file.Close();
}
if(File.Exists(Application.dataPath + "/SaveFiles/Slot VIII.Save")){
File8 = true;
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.dataPath + "/SaveFiles/Slot VIII.Save", FileMode.Open);
SaveFileHandler.SaveGames[8] = (SaveValues)bf.Deserialize(file);
file.Close();
}
if(File.Exists(Application.dataPath + "/SaveFiles/Slot IX.Save")){
File9 = true;
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.dataPath + "/SaveFiles/Slot IX.Save", FileMode.Open);
SaveFileHandler.SaveGames[9] = (SaveValues)bf.Deserialize(file);
file.Close();
}
if(File.Exists(Application.dataPath + "/SaveFiles/Slot X.Save")){
File10 = true;
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.dataPath + "/SaveFiles/Slot X.Save", FileMode.Open);
SaveFileHandler.SaveGames[10] = (SaveValues)bf.Deserialize(file);
file.Close();
}
if(File.Exists(Application.dataPath + "/SaveFiles/Slot AUTO.Save")){
FileA = true;
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.dataPath + "/SaveFiles/Slot AUTO.Save", FileMode.Open);
SaveFileHandler.SaveGames[11] = (SaveValues)bf.Deserialize(file);
file.Close();
}
}
public static void Save(int x) {
string address = ""; // Somehow its an error unless i assign it something...
if(x == 1){
address = "/SaveFiles/Slot I.Save";
}
if(x == 2){
address = "/SaveFiles/Slot II.Save";
}
if(x == 3){
address = "/SaveFiles/Slot III.Save";
}
if(x == 4){
address = "/SaveFiles/Slot IV.Save";
}
if(x == 5){
address = "/SaveFiles/Slot V.Save";
}
if(x == 6){
address = "/SaveFiles/Slot VI.Save";
}
if(x == 7){
address = "/SaveFiles/Slot VII.Save";
}
if(x == 8){
address = "/SaveFiles/Slot VIII.Save";
}
if(x == 9){
address = "/SaveFiles/Slot IX.Save";
}
if(x == 10){
address = "/SaveFiles/Slot X.Save";
}
if(x == 11){
address = "/SaveFiles/Slot AUTO.Save";
}
SaveFileHandler.SaveGames[x] = new SaveValues();
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.dataPath + address); //you can call it anything you want
bf.Serialize(file, SaveFileHandler.SaveGames);
file.Close();
}
public static void Load(int x) {
string address= "";
if(x == 1){
address = "/SaveFiles/Slot I.Save";
}
if(x == 2){
address = "/SaveFiles/Slot II.Save";
}
if(x == 3){
address = "/SaveFiles/Slot III.Save";
}
if(x == 4){
address = "/SaveFiles/Slot IV.Save";
}
if(x == 5){
address = "/SaveFiles/Slot V.Save";
}
if(x == 6){
address = "/SaveFiles/Slot VI.Save";
}
if(x == 7){
address = "/SaveFiles/Slot VII.Save";
}
if(x == 8){
address = "/SaveFiles/Slot VIII.Save";
}
if(x == 9){
address = "/SaveFiles/Slot IX.Save";
}
if(x == 10){
address = "/SaveFiles/Slot X.Save";
}
if(x == 11){
address = "/SaveFiles/Slot AUTO.Save";
}
if(File.Exists(Application.dataPath + address)) {
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.dataPath + address, FileMode.Open);
SaveFileHandler.SaveGames[x] = (SaveValues)bf.Deserialize(file);
file.Close();
Debug.Log("Shards: "+ SaveFileHandler.SaveGames[x].shard);
}
}
}
Comment
How about a line# of error and the callStack from console?
Answer by PixelParts · Sep 06, 2015 at 12:45 PM
I'm guessing you're missing an array initialization:
SaveGames = new SaveValues[12];
Callstack would help.